1

This script looks at a network location for a folder name and specific file and then should copy the file to other folder on the network with current timestamp in destination file name.

Can you advise any syntax error or reason why it is not copying?

ECHO on
Title %0

set GETfn=Q:\Cdata\mm_tn
set GETfn=%GETfn: =%
echo GETfn = %GETfn%

set f1=%GETfn%%m%%d%%y%-%hr%%mn%.csv
set f1=%f1: =%
echo f1 = %f1%

copy GETfn.csv Q:\FTP\Sent\%f1%  
dir Q:\FTP\Sent\
Mofi
  • 46,139
  • 17
  • 80
  • 143
Tony77
  • 311
  • 2
  • 5
  • 25
  • 2
    Is that your whole script? Then `%m%`, `%d%`. `%y%`, `%hr%`, and `%mn%` aren't defined. But that shouldn't keep it from copying. What's the error message? – Stephan Dec 17 '20 at 07:00
  • You also need to clarify what you mean by using the term DOS and the tag [[tag:dos]]. Can you confirm which version of DOS you are using, or if you're really using Windows? (Somebody has now removed the DOS references for you, so please let us know if this is really for DOS and not Windows). I would also advise that you should never need this line, `set GETfn=%GETfn: =%`, and could therefore omit it. Additionally, please consider not ordering your date stamps in a non sortable order, it makes much more sense using `YYYYmmDD-HHMM`. – Compo Dec 17 '20 at 11:06

2 Answers2

1

For example, if i have specificfile.csv on a mapped network drive Q:\Cdata\mm_tn\

then using this:

@echo off

:: src -> destination
set src=Q:\Cdata\mm_tn\
set dst=Q:\FTP\Sent\

echo -------------------------------------
echo source dir      ---^> %src%
echo destination dir ---^> %dst%
echo -------------------------------------

:: timestamp 
:: https://stackoverflow.com/questions/1064557/creating-a-file-name-as-a-timestamp-in-a-batch-job
FOR /F %%A IN ('WMIC OS GET LocalDateTime ^| FINDSTR \.') DO (
    @SET B=%%A
)
set timestamp=%B:~4,2%_%B:~6,2%_%B:~0,4%_%B:~8,2%-%B:~10,2%
echo %timestamp%


:: copy from Q:\Cdata\mm_tn\ -> Q:\FTP\Sent\
copy %src%specificfile.csv %dst%%timestamp%_specificfile.csv

the specificfile.csv is copied to Q:\FTP\Sent\ as timestamped file 12_17_2020_12-39_specificfile.csv. Now paths can be easily adjusted for your requirements.

lww
  • 624
  • 1
  • 7
  • 14
1

The second block is:

set GETfn=Q:\Cdata\mm_tn
set GETfn=%GETfn: =%
echo GETfn = %GETfn%

This block first defines the environment variable GETfn with string value Q:\Cdata\mm_tn. Next it uses a string substitution to remove all spaces from the string assigned to environment variable GETfn. That is of course a completely useless command line as the folder path assigned to the environment variable does not contain any space at all on having the batch file written without trailing space(s) on the line above. The last line of this block just outputs the fixed folder path which makes also no real sense.

The third block is:

set f1=%GETfn%%m%%d%%y%-%hr%%mn%.csv
set f1=%f1: =%
echo f1 = %f1%

It defines an environment variable f1 being a concatenation of the strings assigned to the environment variables GETfn (defined above), m, d, y, hr and mn not defined in posted code at all. So f1 is defined with Q:\Cdata\mm_tn-.csv which is of course not right. The next line is again useless as it removes all spaces from the string assigned to environment variable f1 not containing any space at all, except the batch file contains one or more trailing spaces at end of the line above. The third line just outputs the wrong defined environment variable f1.

I recommend to open a command prompt, run set /? and read the output help carefully and completely from top of first to bottom of last page. Next I suggest to read my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It has some useful additional information about the usage of command SET.

Please read carefully and completely my answer on Time is set incorrectly after midnight. Then you should have the knowledge why the following command line is a replacement for the entire posted batch file code.

@if exist "Q:\Cdata\mm_tn\GETfn.csv" for /F "tokens=1-5 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do @copy "Q:\Cdata\mm_tn\GETfn.csv" "Q:\FTP\Sent\GETfn_%%J%%K%%I-%%L%%M.csv" & exit /B

That command line copies the file Q:\Cdata\mm_tn\GETfn.csv to directory Q:\FTP\Sent with new file name GETfn_MMddyyyy-hhmm.csv using current date/time.

I would not use this date format for the destination file name. It would be better to use GETfn_yyyyMMdd-hhmm.csv, or better to read GETfn_yyyy-MM-dd_hh-mm.csv. This is the international date format which has the big advantage that files sorted by name are with such a date/time format sorted at the same time in chronological order. That is very often very helpful. So better in my opinion would be:

@if exist "Q:\Cdata\mm_tn\GETfn.csv" for /F "tokens=1-5 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do @copy "Q:\Cdata\mm_tn\GETfn.csv" "Q:\FTP\Sent\GETfn_%%I-%%J-%%K_%%L_%%M.csv" & exit /B

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • copy /?
  • exit /?
  • for /?
  • if /?
  • robocopy /?

See also: Single line with multiple commands using Windows batch file

Mofi
  • 46,139
  • 17
  • 80
  • 143