|
Answer» Hello, I'm a week into learning batch files and I'm stuck on a few lines in a new file that I'm trying to write.
Here's an except from my code: (I'm having trouble with only 3 lines of it however: the TWO "set" lines and the "if" statement line.
Can someone give me some help with this?
for /F "delims==" %%d in ('type "%topfolder%\Assign1\dates.txt"') do (
cd %topfolder%\Assign1\%%d
dir /ad /b > locations.txt
for /F "delims==" %%l in ('type "%topfolder%\Assign1\%%d\locations.txt"') do (
for /F "delims==" %%s in ('type "%topfolder%\site files\sitefiles.txt"') do (
set templ=%%l set temps=%%s
if %templ:~1,2% == %temps:~2,2% (copy %topfolder%\site files\%%s %topfolder%\%%d\%%l)
) ) )You've been posting this at AfterDawn, haven't you? Maybe you'll get a better CLASS of help on here...
Put this at the beginning of your batch file
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "delims==" %%d in ('type "%topfolder%\Assign1\dates.txt"') do (
cd %topfolder%\Assign1\%%d
dir /ad /b > locations.txt
for /F "delims==" %%l in ('type "%topfolder%\Assign1\%%d\locations.txt"') do ( for /F "delims==" %%s in ('type "%topfolder%\site files\sitefiles.txt"') do (
set templ=%%l set temps=%%s
(1) You cannot do this in a loop.
if %templ:~1,2% == %temps:~2,2% (copy %topfolder%\site files\%%s %topfolder%\%%d\%%l)
You have to do this.
You don't need the rackets around the if statement, I think.
if !templ:~1,2! == !temps:~2,2! copy %topfolder%\site files\%%s %topfolder%\%%d\%%l ) ) )Yes I have been posting this on AfterDawn. I'm new to posting on forums, and have been looking around. I couldn't believe at first that there were just people out there just helping other people with problems like this! This is something I'm glad I found!
Anyways back to my problem.
I have made the changes you have suggested. The program hasn't STARTED to work yet... so I have 3 further q's:
1) Is the addition of SETLOCAL ENABLEDELAYEDEXPANSION to take care of my problem with the two "set" lines?
2) I recieve the following error in the cmd prompt: "2! was unexpected at this time" Do you have any further advice?
3) And finally, is it possible to disregard the set statements and just use the two variable %%s and %%l in the if statement?
Thanks!Perhaps you also need to know that the variables %%s and %%l are of the form: I17CI and I17CI.site, respectively.Try quotes in these places, aweathe...
if "!templ:~1,2!" == "!temps:~2,2!" copy "%topfolder%\site files\%%s" "%topfolder%\%%d\%%l"
PS How is Indochine Hey contrex, Ya I would have used the same user name with AfterDawn too had they allowed longer usernames. haha And yes Indochine is great! he has been a huge help so far! Of course if you or someone else is able to help me solve my problem then I'll make sure he knows he doesn't need to spend any more time on it.
So back to my script! :
I tried the quotes.. without success. I think if I could make the two "set" statements work properly I would have no problems. Do you know why these two statements make no change to the variables templ and temps? Ie: the variables don't get set! Can you tell me why?
thanks I saw what you wrote on Afterdawn about the two files. It is different from what you wrote here.
AfterDawn Quote the ordering of the filenames is always the same for the two list; first file: one letter, two figures, two letters
second file: two letters, two figures and ".site"
here QuotePerhaps you also need to know that the variables %%s and %%l are of the form: I17CI and I17CI.site, respectively.
The string slicing attempts with temps and templ make more sense if the former is the true case.
QuoteIe: the variables don't get set! Can you tell me why? Variables in FOR statements are EVALUATED once: before the command is executed. Changes made to the variable aren't visible inside the command.
You can use SETLOCAL and ENABLEDELAYEDEXPANSION and variables with ! instead of % to get around this.
If you (a) examine (b) ponder and (c) run this code I'm sure you'll get the idea, and maybe if you incorporate elements of it in your code, you can progress a bit further, and we can see if your COPY OPERATION is performed OK.
Code: [Select] @echo off REM put this at the start of your batch SETLOCAL ENABLEDELAYEDEXPANSION
echo M13AZ> locations.txt echo G99QR>> locations.txt echo I17CI>> locations.txt
echo QP44.site> sitefiles.txt echo FS15.site>> sitefiles.txt echo JM17.site>> sitefiles.txt
for /f "delims==" %%l in (locations.txt) do ( for /f "delims==" %%d in (sitefiles.txt) do (
REM set environment variables REM as copies of FOR metavariables REM this is allowed in a loop set templ=%%l set temps=%%d
REM but if you use them with percent signs REM they will seem empty REM and so an attempt to slice REM them will give empty results set percentnum1=%templ:~1,2% set percentnum2=%temps:~2,2%
REM however this works REM using delayed expansion set exclamnum1=!templ:~1,2! set exclamnum2=!temps:~2,2!
REM let's see the contents of the variables REM 2 and 4 will be blank as explained above echo 1 %%l %%d echo 2 %templ% %temps% echo 3 !templ! !temps! echo 4 %percentnum1% %percentnum2% echo 5 !exclamnum1! !exclamnum2!
REM now for string comparison REM this works if !exclamnum1! == !exclamnum2! ( echo MATCH )
REM or your desired action REM if !exclamnum1! == !exclamnum2! ( REM copy "Path\file" "folder" REM )
REM However you can't do this, the batch crashes REM with the message 2! was unexpected at this time REM if !templ:~2,2! == !temps:~1,2! echo match echo. ) ) REM END OF CODE
Here is the output:- Code: [Select]1 M13AZ QP44.site 2 3 M13AZ QP44.site 4 5 13 44
1 M13AZ FS15.site 2 3 M13AZ FS15.site 4 5 13 15
1 M13AZ JM17.site 2 3 M13AZ JM17.site 4 5 13 17
1 G99QR QP44.site 2 3 G99QR QP44.site 4 5 99 44
1 G99QR FS15.site 2 3 G99QR FS15.site 4 5 99 15
1 G99QR JM17.site 2 3 G99QR JM17.site 4 5 99 17
1 I17CI QP44.site 2 3 I17CI QP44.site 4 5 17 44
1 I17CI FS15.site 2 3 I17CI FS15.site 4 5 17 15
1 I17CI JM17.site 2 3 I17CI JM17.site 4 5 17 17 MATCH
IT WORKS!!!
|