Solved

More Date Variable / Batch File Difficulties

Posted on 2004-08-03
12
8,781 Views
Last Modified: 2009-07-21
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?
0
Comment
Question by:Tezdread
  • 6
  • 5
12 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 11711028
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...
0
 
LVL 1

Author Comment

by:Tezdread
ID: 11714234
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
0
 
LVL 1

Author Comment

by:Tezdread
ID: 11714319
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!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 11714334
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----------------
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 11714339
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'.
0
 
LVL 1

Author Comment

by:Tezdread
ID: 11715126
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
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 11717579
The reason it didn't work for you is the operating system you're using - it displays the day of the week along with the date.
So looking at your output from date/t:

Wed 08/04/2004

Now, to pull out what we need there, we need to ignore (delim) on both the <space> after "Wed" and the "/" seperating the date.
That's the first task, so your command should be adjusted to read

for /f "tokens=2,3, delims=/ " %a in ('date/t') do if %b%a==0408 echo Date is 0408

So this states to break up our above output into several parts deliimiting on spaces and /'s.
Leaving us with 1=Wed, 2=08, 3=04, 4=2004
But you only asked for 2 & 3, so your results would be 2=08 and 3=04, which is what you want, but you still have to reference both values.
Above you've referenced only %%a.  This (DO IF "%%A" == "0208" echo date is 0208) equates to:

If "08" is equal to "0208" then echo date is 0208, which will never be correct.
That's why I like to assign my own variables to these values...
Pull out tokens 1 through 4 and you have
%dow% (%a) = Mon
%month% (%b) = 08
%day% (%c) = 04
%year% (%d) = 2004

You can start with any letter, but you could run into a bit of confusion if you 'start' with %%z as your dow (day of week) and then trying to reference the next 'token' (where do you go from "Z"?? :)

So, with that said - and again, not knowing gzip - so it might help clarify this if you can tell me:
When gzip unzips the compressed DDMM.Z file - does it pull certain files out of there, and what are they, if so?  Or is this one file that is being uncompressed?
(borrowing %yesterday% code from my favorite DOS programmer, SteveGTR:http:Q_20578217.html)

::---------begin code---------
@echo off
::establish temporary environment variables
for /f "tokens=1-4 delims=/ " %%a in ('date/t') do (
set dow=%%a
set month=%%b
set day=%%c
set year=%%d
)
for /f %%x in ('dir c:\originaldirectory\%dow%*.z /b') do (
copy c:\originaldirectory\%%x c:\temp\%day%%month%.z

::begin SteveGTR's code (thank you Steve ;)
:: Substract your 1 days here
set /A day=%day% - 1
set /A month=%month% + 0

if /I %day% GTR 0 goto DONE
set /A month=%month% - 1

if /I %month% GTR 0 goto ADJUSTDAY

set /A month=12
set /A year=%year% - 1

:ADJUSTDAY

if %month%==1 goto SET31
if %month%==2 goto LEAPCHK
if %month%==3 goto SET31
if %month%==4 goto SET30
if %month%==5 goto SET31
if %month%==6 goto SET30
if %month%==7 goto SET31
if %month%==8 goto SET31
if %month%==9 goto SET30
if %month%==10 goto SET31
if %month%==11 goto SET30
if %month%==12 goto SET31

goto ERROR

:SET31

set /A day=31 + %day%

goto DONE

:SET30

set /A day=30 + %day%

goto DONE

:LEAPCHK

set /A tt=%year% %% 4

if not %tt%==0 goto SET28

set /A tt=%year% %% 100

if not %tt%==0 goto SET29

set /A tt=%year% %% 400

if %tt%==0 goto SET29

:SET28

set /A day=28 + %day%

goto DONE

:SET29

set /A day=29 + %day%

:DONE

if /i %day% LSS 10 set day=0%day%
if /I %month% LSS 10 set month=0%month%

echo Date is: %month%/%day%/%year%

set month=
set year=
set day=
set tt=

:EXIT
gzip -d c:\temp\%day%month%.z
::---------end code--------

I'm heading out of town but will check back later or tomorrow.  Good luck!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 11721308
: )
Thanx.
0
 
LVL 1

Author Comment

by:Tezdread
ID: 11721809
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 !
0
 
LVL 1

Author Comment

by:Tezdread
ID: 11764288
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>

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 11771335
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..
0
 

Expert Comment

by:forestsweng
ID: 24906559
This is fantastically awesome. Thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now