|
Answer» Hello everybody.
I have an issue trying to write a batch script and I hope you will be ABLE to point me to the right direction.
First of all, I am brand new to batch scripting, and I had to "learn" it just today because I have to do something special with filenames.
Here is the context :
I have xls files which filenames are containing a date. The filenames are like this syntax : 20071107_SOMETHING.xls
I must be able to extract only the date from the filename, get it to a correct format (e.g 2007/11/07) and put it in a text file.
I get through this site to learn the basics and this is what I was able to write for the time being :
Code: [Select]@echo off for /f "tokens=1-2 delims=_" %%d in ('dir /b folder') do rename "test.txt" %%d.txt set filename=%%d echo yass %filename% >> date.txt pause In this simple script, I have a folder (named folder) containing only one xls file. The 'dir /b' command returns me the correct filename. From there I use the for loop to extract only the date (20071107), and for testing purpose I rename a test.txt file just to verify that I am extracting the good string.
This part is working !
But, in the next part, I tried to get the date into a variable %filename and then write to the date.txt file. But this is not working at all, I have something empty instead.... I guess I am doing something wrong and that's why I'm asking for advice.
So what I want to do is getting this date that I extracted and simply put it into a txt file. It would be great if I could also put '/' to form a correct date format...
Thank you for any help ! I think you probably want something like: Code: [Select]@echo off setlocal enabledelayedexpansion for /f "tokens=1-2 delims=_" %%d in ('dir /b folder') do ( rename "test.txt" %%d.txt set filename=%%d echo yass !filename:~0,4!/!filename:~4,2!/!filename:~6,2! >> date.txt ) pauseThank you GuruGary !
That helped me a lot, even for understanding Batch scripting.
But I have some questions about what you wrote, and If you can give me answers or point to the direction of somewhere where I cand find the information, I will be grateful.
- I understood that in my first try I was using a variable name outside the for loop, so it wasn't recognized... but, now I'm wondering why are you using the '!' before each variable name I first thought it was some SORT of escape character (I was thinking it was ^ actually...) but it looks a bit more complicated, could you TELL me more about the use of the '!' character.
- Could you also please tell me what is the UTILITY of ':~', or indicate where I can find information about it. It looks like an assignment operator , is that right ?
Anyway, thanks for your answer and your help.
I'm still working on my batch script for 2 days now, I'm learning so I'm not doing very fast but I hope I'll be able to finish it correctly.
Bye all !
P.S : sorry if my english is not perfect...Quote could you tell me more about the use of the '!' character.
If a variable is set in a for loop, it must be echoed with !'s around it. Otherwise the variable will be blank. The setlocal enabledelayedexpansion allows variables to be set and echoed in the for loop.
_____________________________________________________________________
Quotetell me what is the utility of ':~' WELL, to my understanding, it is used to extract certain parts of a set variable, like so:
Code: [Select]set numbers=12345 echo %numbers:~2,4%
Would output 234
____________________________________________________________
However the
Code: [Select]:~2,4
represents characters 2 through 4 in that string. Thanks, BFC ... I have been too busy to check the board all day today until now.
BFC got it right. When reading variables in a FOR loop that were set within the same FOR loop, you need to have the "setlocal enabledelayedexpansion" before the loop, and then access the variables with ! instead of % like you normally do outside the loop. And as BFC explained the :~ in a variable is to extract a substring (breaking out the date into its sub-components to add the slashes). For more information on both, do Code: [Select]set /? Quote from: BatchFileCommand on February 24, 2009, 06:54:10 PM Quotetell me what is the utility of ':~' Well, to my understanding, it is used to extract certain parts of a set variable, like so:
Code: [Select]set numbers=12345 echo %numbers:~2,4%
Would output 234
Slight correction - it would output 345
QuoteHowever the
Code: [Select]:~2,4
represents characters 2 through 4 in that string.
Another correction -
It represents the 4 characters starting at offset 2 from the start (i.e. the third character) But in this case there are not 4 characters left, so it halts at the end.
the syntax is
Code: [Select]set substring %string:~A,B% where:
A is the offset from the start (the first character is at offset 0 [zero])
B is the number of characters to take
The alternative is to use a negative number for A, then the counting is from the end of the string
%string:~-4,2% represents the 2 characters starting at the 4th character from the end.
%string:~-1,1% is the last char of the string
All this can be easily seen through experiment.
Thanks for clarifying that Dias.
|