1

I have source and target path. From source path any .xls file would copy to target path and will rename "data.csv"

Suppose the file not copied or renamed. I want to include a command in my script that shows "data.csv" not copy or rename due to zyx reason.

So I can track & fix the issue.

My coding:

@echo off
setlocal
set DateFolder=04.2013
set TargetFolder=F:\Financial\Data\%DateFolder%\Final Reports

:: copy the newest file from AccruntPnLMTD and rename it to PNL.csv
call :copyAndRename "F:\Financial\Data\Reports\AccruntPnLMTD" "%TargetFolder%\PNL.csv"

:: copy the newest file from AccountPnlMTD and rename it to AC.csv
call :copyAndRename "F:\Financial\Data\Reports\AccountPnlMTD" "%TargetFolder%\AC.csv"

:: copy the newest file from ExpensesMTD and rename it to EXPMTD.csv
call :copyAndRename "F:\Financial\Data\Reports\ExpensesMTD" "%TargetFolder%\EXPMTD.csv"


:: Done
goto :eof

:copyAndRename
set SourceFolder=%~1
set TargetFile=%~2

:: Find the newest file in the source folder
for /f "tokens=*" %%F in ('dir /b /od /a-d "%SourceFolder%"') do set "NewestFile=%%F"

:: copy and rename it to the target
copy "%SourceFolder%\%NewestFile%" "%TargetFile%"


:: Done with this subroutine
goto :eof
tshepang
  • 10,772
  • 21
  • 84
  • 127
Gunu
  • 13
  • 3
  • Have you tried anything yet? – GOTO 0 Dec 06 '13 at 10:03
  • yes, but coud't fixed the problem – Gunu Dec 06 '13 at 10:38
  • 1
    Could you at least show the code you're using to copy and rename your files? I'm asking because I'm not quite clear what you're actually doing. There can be only one file named `data.csv` in a folder. So what would you do if you had multiple .xls files to rename? Also, are you renaming the source or target file after copying? – GOTO 0 Dec 06 '13 at 11:18
  • Hi...I just update my coding. Also, I am renaming file at target folder. – Gunu Dec 06 '13 at 14:59
  • You build an filetype collision. XSL( or XSLX) is not compatible to be an CSV-File. You should batch an conversion of XSL-Files to CSV instead of blind copy to another format. – tr0y Dec 07 '13 at 18:08

2 Answers2

1

The for and copy commands always print a message when they encounter an error, so there's no need to care about them. If you want to print a warning for each file in your source folder that is not copied because it's older than the latest one, you can do it explicitly with a second iteration.

:copyAndRename
set SourceFolder=%~1
set TargetFile=%~2

:: Find the newest file in the source folder
for /f "tokens=*" %%F in ('dir /b /od /a-d "%SourceFolder%"') do set "NewestFile=%%F"

for /f "tokens=*" %%F in ('dir /b /od /a-d "%SourceFolder%"') do (
    if NOT "%%F" == "%SourceFolder%\%NewestFile%" echo "%%F" not copied because it is an older file.
)

:: copy and rename it to the target
copy "%SourceFolder%\%NewestFile%" "%TargetFile%"

:: Done with this subroutine
goto :eof

Your copyAndRename function assumes that there is always at least one file in each source folder and no subdirectories. If this is not the case, you should add extra logic to your code, otherwise, %NewestFile% will not be set, or it will retain the value of the previous call to copyAndRename, or it will name a directory that copy doesn't copy.

Update

When copy fails, it sets the errorlevel to 1. By testing for an errorlevel, it is possible to print the name of the source file when an error occurs. The error message is provided by copy in the previous line.

:: copy and rename it to the target
copy "%SourceFolder%\%NewestFile%" "%TargetFile%"
if errorlevel 1 echo File "%SourceFolder%\%NewestFile%" not copied.

As explained in this post, the output of the script can be redirected to a file for logging with the syntax > logfile 2>&1. Assuming the log file is F:\Financial\Data as per comment, the command line would become

myscript.bat > F:\Financial\Data 2>&1
Community
  • 1
  • 1
GOTO 0
  • 28,453
  • 18
  • 97
  • 127
  • yes, for and copy itself gives an message when they encounter an error, But only "file not found The system can not find the file specified" In want that instead of this message it should provide the name like "PNL.csv" not copied or renamed with the error message. – also I don't want the message for older files not copied..I am only looking the message for renamed files not done. (for PNL,AC,EXPMTD). in a log – Gunu Dec 07 '13 at 09:01
  • Also how can I save automatically the error message log at a F:\Financial\Data for future record purpose. – Gunu Dec 07 '13 at 09:05
  • can you please include the last command for log in the script. I am not arranging it properly.thx – Gunu Dec 07 '13 at 19:15
  • I am not good with syntax coding, could you plz make message log commands more clear..thx – Gunu Dec 08 '13 at 06:54
  • @Gunu: I added a link to [this question](http://stackoverflow.com/questions/1420965/redirect-stdout-and-stderr-to-a-single-file) in my answer to address some unclarity with the logging syntax. Hope this helps. Otherwise feel free to start a new question to explain your problem. – GOTO 0 Dec 08 '13 at 11:28
0
@ECHO OFF &SETLOCAL
set flag=%random%
(for /f "delims=" %%a in ('dir /b /a-d /o-d') do (
    if not defined %flag% (
        echo copy "sourcefolder\%%~a" "targetfolder">con
        set %flag%=%random%
    ) else (
        echo Not copied: %%~a
    )
))>nocopy.log
type nocopy.log
Endoro
  • 34,892
  • 8
  • 45
  • 61