Link to home
Start Free TrialLog in
Avatar of wrigh066
wrigh066

asked on

I want to rename a dos file...I want to add the system date to the name of the file ( Very Easy and important)

I am going to copy a file from my c temp to my c upload directory. I want to add change the name of
combined.vch to combined( system date).vch.
So all I want to do is add the date to the name of combined.vch. This will be a batch file.  Thank you

copy /Y /B c:temp\*.vch /B c:\upload\combined.vch
Avatar of hes
hes
Flag of United States of America image

Avatar of wrigh066
wrigh066

ASKER

Ok I looked at the link..Thank you,however I still can't get this to work.  Here is what happens.

@ECHO OFF
CLS
copy /Y /B c:temp\*.vch /B c:\upload\combined.vch
SET CURRENTDATE= SET GETSYSDATE=FOR /F " tokens=1,2,3* delims=/,  " %%i IN ('date /T') DO SET CURRENTDATE=%%i%%j%%k%%l
FOR /F " tokens=1,2,3* " %%i IN ('echo %CURRENTDATE%') DO SET GETSYSDATE=%%j%%k%%l
ren c:\upload\combined.vch combined%%GETSYSDATE%%.vch

I am having trouble running this as a .bat file.  When I run this in Dos I run the copy section first and then the rename second.  What I end up with is a file called combined%GETSYSDATE%.vch
It seems that the GETSYSDATE variable is not getting passed. What am I doing wrong?
ASKER CERTIFIED SOLUTION
Avatar of pbarrette
pbarrette

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nothing new, PB's script should do what you want.

The direct answer to your second question in refernce to the %GETSYSDATE%.vch file, has to do with how the command interpreter makes it difficult to know how many percent signs (%) to use at any given time.  It is odd, but there are reasons to the rules that don't seem to have reason:

Normal variables are enclosed inside one at each end, almost totally regardless of where you use them like:
%TODAY%

The ONLY time you use doubles is inside a for loop, and then only for the variables generated by the loop, not the normal ones. Good example from your code:
FOR /F " tokens=1,2,3* " %%i IN ('echo %CURRENTDATE%') DO SET GETSYSDATE=%%j%%k%%l
Ignoring all else this line works fine.  Your %CURRENTDATE% would correctly expand to the FOR loop, then the FOR loop would generate I J and K to hold one each of the first three words separated by space, an L for the rest of the line.  The SET GETSYSDATE=%%j%%k%%l would give you GETSYSDATE holding %%j (2nd word), %%k (3rd word), and %%l (all other words normal) - with no spaces between 2nd, 3rd, and 4th word.  Beautiful job, (If %CURRENTDATE% was "FRI 05 09 2003" GETSYSDATE would be "05092003"
Contrast that to this line:
ren c:\upload\combined.vch combined%%GETSYSDATE%%.vch
Since this is not in a FOR loop and %%G is not setup as a FOR generated variable, it gets expanded as a single text character % followed by the word GETSYSDATE and another single % then .vch, so there's your file name. This line might have fixed it as you wanted:
ren c:\upload\combined.vch combined%GETSYSDATE%.vch

The problem with what I just said, is so many other changes were made to the setup of %CURRENTDATE% before those two lines that I highly doubt it was "FRI 05 09 2003", so again I recommend cut&paste of bp's script with as little as or no change if possible to make it fit your need.

When a FOR loop is typed at a prompt then it's variables only have one % but still none after the variable. all else stays the same. I Won't bother with replaceable parameters like %1 %2 %3 since most learn those before the normal ones, but the Windows Help file has good info on special usage of those as well.

Hope this helps your future scripts.  
This works GREAT!, however one small glich.  When I copy the file and rename it I noticed at the end of the file a | or  was created after all the data.  When this happens I can not run this .vch file in Peoplesoft.  Does this make sense?  Thank you
If you're using
COPY /Y /B C:\TEMP\*.VCH C:\Upload\Combined.vch
Try switching to
COPY /Y /A C:\TEMP\*.VCH C:\Upload\Combined.vch

(As you can see in "copy /?" /B is the switch for binary or data files and /A is for ASCII or text readable files)

gl
(\o/)
Great that worked! The big question is how can I give both K2K and pbarrette points.  You both helped out and I thank you for that.
Thank you.  All I needed to do was take the /B out of the Dos script.  It was adding a carriage return at the end of the file.  When I took out the /B (also tried /A) the file was not copied with the carraige return at the eof.
thx, not in it 4 pts, (\o/)
Hi K_2K,

You may not be in it for the points, but:
https://www.experts-exchange.com/questions/20616314/Points-for-K-2K.html

Thanks,
pb
Hey K_2K,

Those points are still waiting for you..

pb