Variable not Echoing in For Loop - batch-file

I am trying to get the file names using a batch file from a folder but it just doesn't work.
I followed guidelines from here but for some reason this isn't returning anything at all when it should!
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Desktop\UPD\*.txt') DO SET result=%%G
I also tried:
FOR /F "tokens=*" %%G IN (dir /b C:\Users\Desktop\UPD\*.txt') DO SET _result=%%~G
echo %_result% >> %~dp0Outputfile.txt
What I get is:
ECHO is on.
EDIT
Here is what I did so far now:
IF EXIST C:\Users\Nathanael\Desktop\UPD\*.txt (
echo file found >> %~dp0Outputfile.txt
chDIR C:\Users\Nathanael\Desktop\UPD\
dir *.txt /b >> %~dp0Outputfile.txt
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G
echo %result% >> %~dp0Outputfile.txt
)
The output is:
file found
NewVHD.txt
random.txt
ECHO is on.

If the for /f returns multiple files the last one will overwrite the previous in the set
The way cmd.exe parses (code blocks) requires delayed expansion when a var is set and used inside the same code block as is the case with your if exist
So either avoid the code block with reversed logic
IF NOT EXIST "C:\Users\Nathanael\Desktop\UPD\*.txt" Goto :Eof or other label
Or (always indent code blocks to better keep track) :
Setlocal EnableDelayedExpansion
IF EXIST "C:\Users\Nathanael\Desktop\UPD\*.txt" (
echo file found >> %~dp0Outputfile.txt
chDIR "C:\Users\Nathanael\Desktop\UPD\"
dir "*.txt" /b >> %~dp0Outputfile.txt
FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Nathanael\Desktop\UPD\*.txt') DO SET result=%%G
echo(!result! >> %~dp0Outputfile.txt
)
It's a good habit to always enclose pathes in double quotes
to avoid echo is off messages use an other command separator than a space if the var is possibly empty (I used a ( here

FOR /F "tokens=*" %%G IN ('dir /b C:\Users\Desktop\UPD*.txt') DO SET result=%%G
Make sure that the path is correct (for example, perhaps it should be c:\Users\YourName\Desktop\UPD*.txt where YourName is the user name)?

Related

Batch file to append several text documents to one document

I have created a batch file which should do several things, including appending all text documents of a certain format into one text file provided there are more than one text files of this format in the directory. This section of the code is below:
:multiple
SET /a count=0
ECHO.> "%location%\UAV_camera_coords_all.txt"
FOR /r "%location%\Output" %%G in ("UAV_camera_coords_*.txt") do set /a count+=1
IF %count% GTR 1 (
FOR /r "%location%\Output" %%G in ("*.txt") DO (
SET file=%%~G
TYPE "%file%">>"%location%\UAV_camera_coords_all"
)
GOTO :end
It seems that the code is crashing upon reaching the if statement even though the count variable is greater than one. None of the code in the if statement is executed and indeed none of the code which should come after the if statement is executed either. Is there any syntax or other error which may be causing this?
Besides my comment. I assume that you are not really planning on typing the output of *.txt but instead only UAV_camera_coords_*.txt.. if not, feel free to change it back to *.txt
#echo off
for /f "tokens=1,*" %%i in ('dir /s "%location%\UAV_camera_coords_*.txt" ^| findstr "File(s)"') do set cnt=%%i
if %cnt% gtr 1 (
for /f %%a in ('dir /b /s "%location%\UAV_camera_coords_*.txt"') do type "%%~a"
)>"%location%\UAV_camera_coords-all.txt"
Note, I changed the output filename to be -all and not _all as that would type the file onto itself if it was to be a txt file as well.
Edit adding back original answer, before I realised you were recursively searching through directories:
#echo off
for /f "tokens=1,*" %%i in ('dir "UAV_camera_coords_*.txt" ^| findstr "File(s)"') do if %%i gtr 1 type "UAV_camera_coords_*.txt">>"%location%\UAV_camera_coords_all"
Just for the purpose of providing an alternative, this one uses xcopy to check the file count, and copy to merge them.
PushD "%location%\Output" 2>NUL && For /F %%G In ('""%__AppDir__%xcopy.exe" "UAV_camera_coords_*.txt" . /SQL"')Do If %%G Gtr 1 Copy /Y /B "UAV_camera_coords_*.txt" "..\UAV_camera_coords_all.txt">NUL & PopD

Command works on command line, but operates unexpectedly in batch file

I have this simple batch code to check the Date Modified on a sub-folder (specifically the Recycle Bin.)
This search works flawlessly when manually input in Command Prompt, but not batch.
And using the same exact code to check other folders it works just fine. Help?
Code:
if exist C:\$Recycle.Bin (
pushd "C:\$Recycle.Bin"
for /F "delims=" %%a in ('dir /S /b S-1-*-1001 /AD') do set {file}=%%a
for %%a in ("%{file}%") do echo Recycle Bin: %%~ta
popd
)
The reason this is not working in batch is for one annoying feature of IF statements with the SET command. As stated by This Post - "cmd expands variables when commands are parsed, not when they are run. It so happens that an if or for statement with a block ( ... ) (or actually any block) counds as a single command in that case. So when you set variables inside a block and try using them in the same block there are no variables anymore – they were replaced by the values the variables had before the block even executed." - Joey
To fix this you can simply not put your code block inside the IF statement but rather use an ELSE and have it goto an :EOF
Option 1: - Avoid IF Statement W/H Code Block
#ECHO OFF
Rem | Check If Directory Exists & pushd It.
if exist "C:\$Recycle.Bin" (pushd "C:\$Recycle.Bin") ELSE (goto :EOF)
Rem | Grab data on folders
for /F "delims=" %%a in ('dir /S /b S-1-*-1001 /AD') do (set "{File}=%%a")
Rem | Display data on folders
for %%a in ("%{file}%") do (echo Recycle Bin: %%~ta)
Rem | Un-pushd
popd
pause
goto :EOF
If you do however wish to use a block inside the IF statment you will need to use setlocal enabledelayedexpansion at the top of your script. Furthermore, to echo or read brackets you will have to use !{File}! over %{File}%.
Option 2: - Properly expand IF Statement W/H Code Block
#ECHO OFF
#setlocal enabledelayedexpansion
if exist "C:\$Recycle.Bin" (
pushd "C:\$Recycle.Bin"
for /F "delims=" %%a in ('dir /S /b S-1-*-1001 /AD') do (set "{File}=%%a")
for %%a in ("!{file}!") do (
Set "data=%%~ta"
echo Recycle Bin: !data!
)
popd
) Else (Goto :EOF)
pause
goto :EOF

Assign the directory of a file to a variable

In CMD I can search a file with the following command:
DIR /S /B PROGRAM.EXE
If the file is found the result will be:
C:\Users\Dev\Desktop\Program.exe
I would like to get in the output just the directory without the file name C:\Users\Dev\Desktop\ to assign only the path to a variable.
Is there any way to do this at CMD?
Use a For loop like this:
For /F "Delims=" %A In ('Dir/B/S/A-D "Program.exe" 2^>Nul') Do #Echo=%~dpA
Double up the % in a batch file.
In a batch file, to set any matches as a variable use this structure:
#Echo Off
Set "i=0"
For /F "Delims=" %%A In ('Dir/B/S/A-D "Program.exe" 2^>Nul') Do (Set/A "i+=1"
Call Set "OnlyPath[%%i%%]=%%~dpA")
Set OnlyPath[
Timeout -1
Each match will be set as a different variable just to ensure that if more than one match is made you retrieve them all.
With a batch file, you can do someting like that :
#echo off
set "Working_Folder=%userprofile%\Desktop"
For /F "Delims=" %%F In ('Dir /B /S /A-D "%Working_Folder%\PROGRAM.exe" 2^>Nul') Do (
Set "MyFolder=%%~dpF"
)
Echo "%MyFolder%" & pause>nul

return the name of a file using a batch script

I am trying to write a script that will search a given directory, find files ending in a .ready_go extension, rar them (saving the rar as the file name.rar) and then move onto the next file with the same extensions and do the same.
I am not having luck assigning a variable "filename" to the name of the file found in the loop. this variable will be used to name the rar archive
so far this is what i have:
set dSource=C:\users\admin\desktop\one
set dTarget=\\filesrv1\archives
set fType=*.ready_go
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
set filename=%%f
echo %filename%
echo %dsource%
pause
)
its echo'ing the dsources variable fine, but will not echo the filename variable.
I also tried just "echo %%f" which also did not work.
some help would be appreciated. thanks.
You need to use delayedexpansion since you are setting the varable inside the loop.
try this:
setlocal enabledelayedexpansion
set dSource=C:\users\admin\desktop\one
set dTarget=\\filesrv1\archives
set fType=*.ready_go
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
set filename=%%f
echo !filename!
echo %dsource%
pause
)

Batch - Combining Two Pieces of Text on One Line in a Loop

I'm trying to work up a batch file to combine two pieces of text on one line. The first is the filename; the second is the first line of text beginning with "To: ". I have been struggling for hours and this is as far as I've gotten:
#echo off
IF EXIST fullnames.txt DEL fullnames.txt
FOR /F %%g IN ('dir /b *.eml') DO (
SET filename=%~f1
SET toline=FINDSTR /B /C "To: "
ECHO %FILENAME%%TOLINE% >> fullnames.txt
)
and it doesn't work. I am getting errors or incorrect results almost regardless of what I put down for the filename line; haven't even begun to test the toline part. Any suggestions?
You already used FOR /F to capture the output of the DIR command. Capturing the output of FINDSTR is no different.
However, it is more efficient to use a simple FOR in place of the FOR /F with the DIR command.
You used %~f1 when I think you intended %%~fg.
You cannot expand a variable set within parentheses using %var%, you must use !var! delayed expansion instead. Type SET /? from the command line for more information - read the section starting with "Finally, support for delayed environment variable expansion has been
added..."
However, in your case, you can easily avoid using delayed expansion (not that it is a problem).
Instead of deleting any existing "fullnames.txt" and then appending output to it, it is more efficient to enclose the entire construct within parentheses and redirect all output to the file using the over-write mode.
#echo off
(
for %%F in (*.eml) do (
for "delims=" %%A in ('findstr /b /c:"To: " "%%F"') do echo %%F %%A
)
) >fullnames.txt
But the above solution, simple as it is, is much more complicated than it needs to be.
FINDSTR can process multiple files specified with wildcards, and it will prefix each matching line with the filename followed by a colon.
You can get your results simply from the command line without even using a batch file (or you could put this in a batch file):
findstr /b /c:"To: " *.eml >fullnames.txt
Edit
If you are concerned that a file might contain multiple lines starting with "To: ", and you only want to use the first line, then it is back to using a batch file:
#echo off
setlocal enableDelayedExpansion
set "prevFile="
(
for /f "tokens=1* delims=:" %%A in ('findstr /b /c:"To: " *.eml') do (
if "%%A" neq "!prevFile!" echo %%A: %%B
set "prevFile=%%A"
)
) >fullnames.txt
The above solution could fail if a filename contains !. Also, a path could be used with *.eml as long as the path does not contain a drive letter. Both the drive and ! issues can be resolved with additional modifications.
#echo off
setlocal EnableDelayedExpansion
if exist fullnames.txt del fullnames.txt
for %%f in (*.eml) do (
set toline=
for /F "delims=" %%l in ('findstr /B /C:"To: " "%%f"') do (
if not defined toline set "toline=%%l"
)
echo %%f!toline! >> fullnames.txt
)
EDIT: Simpler method added
The set toline= command delete 'toline' variable before each file is processed, so just the first "To: " matching line found is assigned to it and later shown using Delayed Expansion. However, this process may be achieved in a simpler way that doesn't require Delayed Expansion, as dbenham suggested:
#echo off
if exist fullnames.txt del fullnames.txt
for %%f in (*.eml) do (
set firstFind=
for /F "delims=" %%l in ('findstr /B /C:"To: " "%%f"') do (
if not defined firstFind set firstFind=now & echo %%f%%l >> fullnames.txt
)
)
You can't assign and use environment variables inside a for loop. Use delayed variable expansion or call a subroutine.
Delayed would look something like this
setlocal EnableDelayedExpansion
#echo off
IF EXIST fullnames.txt DEL fullnames.txt
FOR /F %%g IN ('dir /b *.eml') DO (
SET filename=%~f1
SET toline=FINDSTR /B /C "To: "
ECHO !FILENAME!!TOLINE! >> fullnames.txt
)
However, that doesn't look like it would work correctly anyway. I would do it like this
FOR /F %%g IN ('dir /b *.eml') DO call :process %%g
goto :eof
:process
SET filename=%~f1
SET toline=FINDSTR /B /C "To: "
ECHO %FILENAME%%TOLINE% >> fullnames.txt

Resources