Link to home
Start Free TrialLog in
Avatar of Tezdread
Tezdread

asked on

More Date Variable / Batch File Difficulties

Hi all, I seem to have a probalem that's got a few other vistors scracthing their heads.

After reading some online docs and posts I've worked out how to get the relevant info as a variable, this being the first three characters of the date.

DATE/T > C:\Temp\Date.txt (e.g. output - Mon 02/08/2004)
For /F "tokens=1 delims= " %%A IN (C:\Temp\Date.txt) DO IF "%%A" == "Mon" COPY C:\OriginalDirectory\Mon-file.001.Z C:\Temp

That works and copies the right file, so I copied that line so I now have a FOR line for each day of the week and that seems to be fine to.

I put that together from bits that I found online and I understand the tokens and delims part but not the %%A variable, could someone explain that and where to find out what all the other variables are?

Hopefully armed with a list of variables I'll understand it more. Until then, this is the problem

After the above has copied the relevant file, (which always starts like Mon, Tue, Wed etc) I need to rename the file from Mon-file.001.Z to ddmm.Z and then after that unzip the file and again rename it (I don't know an easier way)

I was hoping this would work for the naming part then adapted for the unzip, but not

FOR /F "tokens=2,3 delims=/" %%A IN (c:\temp\Date.txt) DO IF "%%A" == "0208" rename "%TEMP%\Mon*.*.Z" %%A.Z

Could someone explain how to get the ddmm from the same date.txt file to use with the rename and unzip?
Avatar of sirbounty
sirbounty
Flag of United States of America image

I'm not sure I'm exactly understanding your issue, but would think it would be simpler to use:

for /f "tokens=1 delims= " %%a in ('date/t') do copy c:\originaldirectory\%%a-file.001.z c:\temp

No need to create the output file.
The %%a references the information that comes from what's inside the parens ().  You use a single % if running the same command from a dos prompt and double %% when run inside a batch file.

Can you further explain why you need to rename a file, then rename it again and then unzip and rename it?  That's where I've lost you...
Avatar of Tezdread
Tezdread

ASKER

Ok output file done with.

You say "The %%a references the information that comes from what's inside the parens ()." Which I understand, what I don't get it what the value actually means? I have seen others in similar scripts that use %%I or %%i and they mean different things.

When I tried using %%A to capture a different part of the date (ddmm) to rename the file it would only give me DD and not both. I then tried different combinations like %%A%%B %%A%B to capture both but these didn't work.

The renaming - Thinking about it now I could probably get away with renaming once. The reason for the various renames is partly because I didn't think it would be possible to rename the file in the same command line as the copy because the tokens / delims is already being used to copy the file.

So a rename would have to take place on a separate command line to use tokes / delims to get the DDMM and rename the file. The other reason for the rename is because when I run the "GZIP -D" command on the file it removes the .Z and leaves a file with no extention so the final rename would be to add the extention. As I say after thinking this through I could rename the file so it shows the required extention just before the .Z

So it would be from Mon-File.001.Z to DDMM.EXT.Z and then when I run the "GZIP -D" command the file will be in the correct format (0408.lwl)

Hope this helps
Sorry I forgot to mention one very important part (and possible the most difficult)...The date DDMM to be used in the file name (0408.lwl) should NOT be todays date but yesterday date. Should have mentioned that first!
Have you tried
 FOR /?
from a command prompt?  This will give you the most exhaustive definition, but maybe this is a good way to sum up:

For %a in (See Spot run) do echo %a
will cycle the echo command through each item within ().  So, you're output will be

See
Spot
run

There are additional methods when using the /f (and other) switches.  You can reference the output of a command by placing single quotes around the data as I've outlined above.  You can directly reference a system/other variable by placing double quotes around it.

The letter that you use doesn't matter near as much, my personal preference is to start with %%a as the variable.

But I still think I'm completely understanding your efforts here.  Could you describe what you're attempting without using a batch file?

The way I understand it thus far is, you copy file C:\originaldirectory\mon-file-001.z to C:\temp
Then, you rename the file in C:\temp to 0408.Z and then unzip the file and rename it to something else (what?).

You can accomplish this in one pass through of a batch file, if I understand correctly (not familiar with gzip, but using the command reference above):

::-------Begin code-------------------
@echo off
for /f "tokens=1-3 delims=/ " %%a in ('date/t') do (
set dow=%%a
set month=%%b
set day=%%c
)
copy c:\originaldirectory\%dow%-file-001.Z c:\temp\%day%%month%.Z
gzip -d c:\temp\%day%month%.z
::Now rename to what?
::------------End code----------------
Well that certainly twists it around completely.
Let me know that last rename command you're after and I'll submit the revised code for 'yesterday'.
sirbounty,
The help was the first place I looked but it's far from easy to follow (for me) so had to turn to examples online.

If any letter like &&A can be used, why didn't this:
FOR /F "tokens=2,3 delims=/" %%A IN ('date/t') DO IF "%%A" == "0208" echo date is 0208
echo the date when it's getting this info
Mon 02/08/2004
I was under the impression that the tokens part would look at 02 & 08 (tokens 2,3) ignoring the delims /
but when I try, it only returns 02?

The explanation
On Monday morning all the files in OriginalDir will be checked, any file names starting with the current day of the week and ending in .Z will be copied to the C:\temp dir.
Next, the file that's been copied will need to be renamed to DDMM.lwl.Z (Where DD is yesterday's date)
The file will then need to be unzipped and this will leave the file in the correct format DDMM.lwl.

Hope this explains it
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

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
: )
Thanx.
I haven't had chance to work with this yet but I trust that it'll work and the explanation is great. I may have another question or but I'll have a play and see what I can work out.

One thing I was thinking, when I looked at steve's post he only got 50 points and as part of the solution to my problem was from him can you so anything to split with steve? :-)

Thanks for sorting this out !
Hi sirbounty

I'm having difficulties with getting the above to work...(after fixing the deliberate mistake ;-)

This is what's happening (echoed), the first bit doesn't see the file to copy and the next part I think is wrong is the date at the end of Steve code. Could you take a look and advise?

Thanks

C:\WINNT>test2

C:\WINNT>for /F "tokens=1-4 delims=/ " %a in ('date/t') do (
set dow=%a
 set day=%b
 set month=%c
 set year=%d
)

C:\WINNT>(
set dow=Tue
 set day=10
 set month=08
 set year=2004
)

C:\WINNT>set odir=o:\team folders\support teams\opps supp\knowledge engineer\transcripts

C:\WINNT>set tmp=c:\temp\self service\wss kb

C:\WINNT>for /F %x in ('dir o:\team folders\support teams\opps supp\knowledge engineer\transcripts\Tue*.z /b')
 do (copy "o:\team folders\support teams\opps supp\knowledge engineer\transcripts\%x" "c:\temp\self service\ws
s kb\1008.lwl.z" )
The system cannot find the file specified.

C:\WINNT>set /A day=10 - 1

C:\WINNT>set /A month=08 + 0
Invalid number.  Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

C:\WINNT>if /I 9 GTR 0 goto DONE

C:\WINNT>if /I 9 LSS 10 set day=09

C:\WINNT>if /I 08 LSS 10 set month=008

C:\WINNT>echo Date is: 008/09/2004
Date is: 008/09/2004

C:\WINNT>set month=

C:\WINNT>set year=

C:\WINNT>set day=

C:\WINNT>set tt=

C:\WINNT>gzip -d "c:\temp\self service\wss kb\.lwl.z"
c:\temp\self service\wss kb\.lwl.z: No such file or directory

C:\WINNT>

Sorry - been out of town a few days...

This line:

for /F %x in ('dir o:\team folders\support teams\opps supp\knowledge engineer\transcripts\Tue*.z /b')
 do (copy "o:\team folders\support teams\opps supp\knowledge engineer\transcripts\%x" "c:\temp\self service\ws
s kb\1008.lwl.z" )

is referencing LFNs and should either be converted or have the folder/string enclosed in quotes...

'dir "o:\team folder\[etc, etc] or, if you'd rather use the 8.3 naming structure,
'dir o:\teamfo~1\suppor ~1\oppssu~1\knowle~1\transcripts\Tue*.z /b' [etc. etc]

I haven't used Steve's "yesterday" code, so I'll play around with that and see what I can come up with..
This is fantastically awesome. Thanks!