Batch file. Variable input from a file not working - variables

So heres what I'm trying to do. I'm taking the variable from a text file they look like this
000001 server1 server2
000002 server2 server1
the 1st set of code i wrote would execute but only the last line of the text file
Set _InputFile=c:\usermovelist.txt
For /F "tokens=1-3* delims= " %%A IN (%_InputFile%) DO (
Set _var1=%%A
Set _var2=%%B
Set _var3=%%C
)
xcopy \\%_var2%\e$\profiles\%_var1%.V2 \\%_var3%\e$\profiles\%_var1%.V2 /m /e /y /O /h
xcopy \\%_var2%\e$\users\%_var1% \\%_var3%\e$\users\%_var1% /m /e /y /O /h
pause
ren \\%_var2%\e$\profiles\%_var1%.V2 %_var1%.old.V2.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
ren \\%_var2%\e$\users\%_var1% %_var1%.old.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
So then I tried this
Set _InputFile=c:\usermovelist.txt
For /f "tokens=* delims= " %%f in (%_InputFile%) do (
set line=%%f
call :processToken
)
:processToken
For /F "tokens=1-3* delims= " %%A IN (%line%) DO (
Set _var1=%%A
Set _var2=%%B
Set _var3=%%C
)
xcopy \\%_var2%\e$\profiles\%_var1%.V2 \\%_var3%\e$\profiles\%_var1%.V2 /m /e /y /O /h
xcopy \\%_var2%\e$\users\%_var1% \\%_var3%\e$\users\%_var1% /m /e /y /O /h
pause
ren \\%_var2%\e$\profiles\%_var1%.V2 %_var1%.old.V2.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
ren \\%_var2%\e$\users\%_var1% %_var1%.old.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
But this doesnt read the variables into the xcopy part of it but it reads line by line which i also need it to do. I can hit any key and it moves down each line
C:\Users\xxxxx\Desktop>For /F "tokens=1-3* delims= " %A IN (000002 server2 server1) DO (
Set _var1=%A
Set _var2=%B
Set _var3=%C
I'm new to this so thanks in advance for any help!Sorry for the long post I bolded the good stuff

Remove unneeded variables
Set "_InputFile=c:\usermovelist.txt"
For /F "usebackq tokens=1-3* delims= " %%A IN ("%_InputFile%") DO (
xcopy \\%%B\e$\profiles\%%A.V2 \\%%C\e$\profiles\%%A.V2 /m /e /y /O /h
xcopy \\%%B\e$\users\%%A \\%%C\e$\users\%%A /m /e /y /O /h
pause
ren \\%%B\e$\profiles\%%A.V2 %%A.old.V2.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
ren \\%%B\e$\users\%%A %%A.old.%int%.%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
)

Related

How to fix renaming methods ? "The systems cannot find the file specified"

I've tried to rename file from someFile_v4 to someFile_v5 in all folders. Unfortunately I've get error The system cannot find the file specified.
#echo off
for /R %%f in (*_v4.xml) do call :ProcessFile
goto :Finished
:ProcessFile
rename "*_v4.xml" "*_v5.xml"
goto :eof
:Finished
The problem here is that you are using a for loop, which actually does nothing with the variable in this.
Try the following:
(
I also suggest you to consider changing:
for /r to for /f in using dir /s /b /a-d in the loop.
)
#echo off & setlocal EnableDelayedExpansion
for /f "tokens=* delims= " %%i in ('dir /s /b /a-d "*_v4.xml"') do (
set "_ren=%%~fi"
for /f "delims=" %%I in ('dir /w /b "%%~i"') do set "_to=%%~nxI"
ren "!_ren!" "!_to:_v4=_v5!"
set _ren=0<nul & set _to=0<nul
)
I cannot explain my code, sorry, because I don't know English well.

Extract line below Findstr criteria in Batch file for cycle

I'm trying to extract from a set of txt files the line
placed 2 rows below the one matching search criteria
and redirect output to a csv file.
I managed to do that for a specific txt file in the set,
but I'm getting troubles in writing the For Cycle
to batch-scan each txt in a given folder.
Through this, I wrote the following code to scan
a specific file. It works fine:
setlocal EnableDelayedExpansion
cd "myFolder"
if exist myOutput.csv del myOutput.csv
for /F "delims=:" %%A in ('findstr /B /N /C:"myCriteria" "myFile.txt"') do (
set /A LineBelow=%%A+2
set "LineBelow=!LineBelow!: "
)
(for /F "tokens=1* delims=:" %%A in ('findstr /N "^" "myFile.txt" ^| findstr /B "%LineBelow%"') do ^
echo %%B>>myOutput.csv)
start myOutput.csv
ENDLOCAL
When I tried to generalize the code in a For Cycle to scan each txt in myFolder
I got an error in Findstr: !LineBelow! happens to be an empty variable...
Here's the flawed For Cycle:
setlocal EnableDelayedExpansion
cd "myFolder"
if exist myOutput.csv del myOutput.csv
for %%F IN ("*.txt") do (
(
for /F "delims=:" %%A in ('findstr /B /N /C:"myCriteria" "%%F"') do (
set /A LineBelow=%%A+2
set "LineBelow=!LineBelow!: "
))
(for /F "tokens=1* delims=:" %%A in ('findstr /N "^" "%%F" ^| findstr /B "!LineBelow!"') do ^
echo %%B>>myOutput.csv))
start myOutput.csv
ENDLOCAL
Could anybody help me in correcting this code?
Thanks
#echo off
setlocal enableextensions enabledelayedexpansion
cd "myFolder"
> myOutput.csv (
for /f "tokens=1,2 delims=:" %%a in ('
findstr /b /n /c:"myCriteria" *.txt
') do (
set /a "line=%%b+2"
for /f "tokens=1,* delims=:" %%c in ('
findstr /n "^" "%%a" ^| findstr /b /c:"!line!:" 2^>nul
') do echo(%%d
)
)
This uses findstr to directly enumerate all the lines matching the myCriteria and include in the output the name of the file (%%a) and the number of the line (%%b). This information is used to retrieve the final line (%%d) from the matching files.

1 Character in path changing... How to Handle?

I have a path:
\%VARIABLE%svr0001\e$\Users\%VARIABLE%POS00??\E2ELOGS*.dbg
The ONLY characters that changes is the '??' (%VARIABLE%POS00??).
Values could be from 01 through 15 (01,02,03,04,05,06,07,08,09,10,11,12,13,14,15)
How would I make the following script work without creating if path exist statements for every potential changing value:
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('find /c /i "Error : -2" "\\%VARIABLE%svr0001\e$\Users\%VARIABLE%POS00??\E2ELOGS\*.dbg"') do (
set "$line=%%a"
set "$lastchar=!$line:~-1!"
if !$lastchar! gtr 0 echo %%a >>NO_ACK_ERROR-2.txt
)
Try like this :
#echo off
setlocal enabledelayedexpansion
for /l %%x in (1,1,15) do (
set "$number=0%%x"
for /f "delims=" %%a in ('find /c /i "Error : -2" "\\%VARIABLE%svr0001\e$\Users\%VARIABLE%POS00!$number:~-2!\E2ELOGS\*.dbg"') do (
set "$line=%%a"
set "$lastchar=!$line:~-1!"
if !$lastchar! gtr 0 echo %%a >>NO_ACK_ERROR-2.txt
))
If I understood it correct, you want to check the log files with the name POS001-POS0015, below may be the answer for you.
setlocal enabledelayedexpansion
for /l %%i in (1,1,15) do (
set "num=%%i"
if "!num:~0,1!" neq "0" set "num=0!num!"
set "filepath=\\%VARIABLE%svr0001\e$\Users\%VARIABLE%POS00!num!\E2ELOGS\*.dbg"
for /f "delims=" %%a in ('find /c /i "Error : -2" "!filepath!"') do (
set "$line=%%a"
set "$lastchar=!$line:~-1!"
if !$lastchar! gtr 0 echo %%a >>NO_ACK_ERROR-2.txt
)
Cheers, G

Using a bat file to create a log

I am trying to monitor the install of software on our servers remotly with a script by checking the .dll file version. I am not sure if this can be done but after the install on all of the servers I would like to run a bat file to look at all of the servers and create a log file with the server name and that .dll file version number. Suggestions would be great. Thanks.
First download STRINGS.EXE from here (to handle unicode).
http://technet.microsoft.com/en-us/sysinternals/bb897439.aspx
Here is a little something I have been using for quite a while. It does a bit more than you asked for but maybe someone else would prefer to have all of this. You can easily delete the parts you don't want. Add/remove file types as you like and remove the output sections you don't want. It does not do exactly what you asked but the hard part is done and you can adapt as you see fit. Save the code as FileVersions.bat (or whatever). Put this bat file and the downloaded STRINGS.EXE in the same folder. Execute this bat file to get output in FileVersions.txt (or whatever you name bat file). It will look something like this. Gets versions for all specified file types in folders underneath the current folder. Get this modified as you desire and then handle multiple machines as you see fit.
====== Output ============
Versions for *.dll *.ocx *.exe files - Thu 08/08/2013 21:13:28.17
File=C:\Drivers\storage\strings.exe
FileDesc=strings
FileVer=2.41
ProductName=Sysinternals Strings
ProductVer=2.41
File=C:\Drivers\storage\R159108\TEACico2.dll
FileDesc=TEACico2.DLL
FileVer=1, 1, 0, 0
ProductName= TEACico2 DLL
ProductVer=1, 1, 0, 0
====== End of Output ============
#ECHO OFF
TITLE %~n0
PUSHD %~dp0
SET FileTypes=*.dll *.ocx *.exe
SET OutFile=%~n0.txt
IF EXIST %OutFile% DEL /q %OutFile%
ECHO.Retrieving file versions for %FileTypes% files
ECHO.
ECHO.Versions for %FileTypes% files - %Date% %Time% > %OutFile%
ECHO.
FOR /R "%CD%" %%A IN (%FileTypes%) DO ECHO.%%A & CALL :FileVersion "%%A" >> %OutFile%
ECHO.
ECHO.Results are in %OutFile%
pause
GOTO :eof
REM ========================= Subroutines =========================
:FileVersion
SETLOCAL
ECHO.
ECHO.File=%~1
SET LineNum=Invalid
FOR /F "tokens=1 delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND /I "FileDescription"') DO SET LineNum=%%A
IF %LineNum%==Invalid GOTO :FileVer
SET /A LineNum += 1
FOR /F "tokens=1* delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND "[%LineNum%]"') DO SET FileDesc=%%B
IF NOT "%FileDesc%"=="FileVersion" SET FileDesc
:FileVer
SET LineNum=Invalid
FOR /F "tokens=1 delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND /I "FileVersion"') DO SET LineNum=%%A
IF %LineNum%==Invalid GOTO :ProductName
SET /A LineNum += 1
FOR /F "tokens=1* delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND "[%LineNum%]"') DO SET FileVer=%%B
SET FileVer
:ProductName
SET LineNum=Invalid
FOR /F "tokens=1 delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND /I "ProductName"') DO SET LineNum=%%A
IF %LineNum%==Invalid GOTO :ProductVer
SET /A LineNum += 1
FOR /F "tokens=1* delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND "[%LineNum%]"') DO SET ProductName=%%B
IF NOT "%ProductName%"=="ProductVersion" SET ProductName
:ProductVer
SET LineNum=Invalid
FOR /F "tokens=1 delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND /I "ProductVersion"') DO SET LineNum=%%A
IF %LineNum%==Invalid GOTO :Done
SET /A LineNum += 1
FOR /F "tokens=1* delims=[]" %%A IN ('STRINGS %1 ^| FIND /N /V "" ^| FIND "[%LineNum%]"') DO SET ProductVer=%%B
SET ProductVer
:Done
ENDLOCAL
GOTO:EOF

Batch: for /f +xcopy output - Save to .log

I have the next script, and I need it to save all the xcopy files copy outputs to one log file,
:tmdeploy
title Deploying Edithor - %deployer%
set src_folder=S:\ApliTelinver\Compilacion\Edithor 10.5\CompilacionQA
set dst_folder=S:\ApliTelinver\Ambientes-Edithor\Sincronizacion\Test\Test-Mantenimiento
set filelist=filelist-tm.txt
echo Origen: %src_folder%
echo Destino: %dst_folder%
echo.
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
)
echo.
pause
goto end
The problem is that I only get the last file copy in the output. And how to properly do a timestamp for .log file?
Thank you
You should use the appended redirection operator, >> instead of >.
So, your for loop will look like this:
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
)