i am trying to get files from a directory and want to set names of the file to a variable using batch script.
this is my code .but it always setting same value to variable
can any body give solution
echo on
setlocal EnableDelayedExpansion
for /f %%x in ('dir /b C:\backup_dir') do (
SET test=%%~nx
if "%test:~0,6%"=="kdc_db" (set DUMP=%%x)
if "%test:~0,6%"=="kdc_ke" (set KEYS=%%x)
)
echo %DUMP%
echo %KEYS%
here dump and keys variables are always set to same value
You need to use delayed expansion. You have already enabled it, you just need to replace your %'s with !'s
echo on
setlocal EnableDelayedExpansion
for /f %%x in ('dir /b C:\backup_dir') do (
SET test=%%~nx
if "!test:~0,6!"=="kdc_db" (set DUMP=%%x)
if "!test:~0,6!"=="kdc_ke" (set KEYS=%%x)
)
echo %DUMP%
echo %KEYS%
Related
I am new to Batch script,
by using the following code
setlocal enabledelayedexpansion enableextensions
set LIST=
for %%x in (D:\all_files\*.csv) do set LIST=!LIST! %%x
set LIST=%LIST:~1%
echo %LIST%
i am getting the filenames with directory and also with a Paragraph
but i need file names alone one by one like below into a Variable of %LIST%
file1.csv
file2.csv
file3.csv
can any one please help us
I hope, I got your intentions right.
Instead of a variable, just loop over output of dir /b
:loop
echo still waiting...
timeout /t 10
set "ok=yes"
for /f "delims=" %%a in ('dir /b "D:\all_files\*.csv"') do (
if not exist "C:\%%a" set "ok=no"
)
if "%ok%" == "no" goto :loop
echo all there...
call process1.bat
Hi I don't have much experience in batch-programming and have a problem. I have a .bat script that reads a file with a list of paths and i want to get the filename of these paths. I use the script in cygwin.
My code in the Script:
for /F %%a in (error1.txt) do (
set value=%%a
FOR /F %%I IN ("%value%") DO SET MYPATHFILE=%%~nxI
)
When i run the Script %value% is empty.
Value of error1.txt:
a/b/c/d/TextIWant
you need delayed expansion or you can directly use %%a:
for /F %%a in (error1.txt) do (
FOR /F %%I IN ("%%a") DO SET MYPATHFILE=%%~nxI
)
or
setlocal enableDelayedExpansion
for /F %%a in (error1.txt) do (
set value=%%a
FOR /F %%I IN ("!value!") DO SET MYPATHFILE=%%~nxI
)
It looks like you will need Delayed Expansion.
The current problem is, that you want to use a variable in the same set of brackets where you changed the value in (the surrounding For-Loop).
Add setlocal EnableDelayedExpansion to your code at the top and change the %value% to !value!
You can test the problem yourself with this code:
#echo off
setlocal EnableDelayedExpansion
set foo=bar
For /L %%a (1,2,1) do (
set foo=foobar
echo.old value %foo%
echo.new value !foo!
)
I hope it helped :)
Greetings
geisterfurz007
Scenario:
We have multiple releases of a product, and for each release, a folder is created in the main folder. A help file is modified in various releases. I have all the help file names listed in a text file.
I need a script to:
Take each file name from the filenames.txt file
Search for the file by that name in the entire directory (in all releases)
Find the latest file
Copy it to a specified folder
I took help from the various pieces of code I found on Stack Overflow, and combined them to get this code:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
echo.
FOR /F "usebackq delims=" %%a in ("filenames.txt") do (
SET "x=%%a"
ECHO '!x!'
SET FFPath=C:\SVN\nlbavwdocsvn\rep_doc_erpln\trunk\ERPLN
SET NewPath=C:\Lavanya\extracted
SET NewestDate=20160824
ECHO Recursively searching %FFPath%
FOR /F %%I in ('DIR %FFPath%\ !x! /a:-d /s /b') DO (
SET FullDate=%%~tI
ECHO %FFPath%
REM Set CurrDate to yyyymmdd format. Note: Will fail if regional settings changed.
SET CurrDate=!FullDate:~6,4!!FullDate:~0,2!!FullDate:~3,2!
If !CurrDate! gtr !NewestDate! (
SET NewestDate=!CurrDate!
SET NewestFile=%%~fI )
ECHO Copying %NewestFile% to %NewPath%
ECHO.
COPY /Y "%NewestFile%" "%NewPath%"
ECHO.
)
)
PAUSE
This code is not working. And I am unable to figure out the error.
Here is a script to search for the most recently modified file, using the wmic command to retrieve the last modification date/time in a locale-independent manner (e. g., 20160824115500.000000+060).
So for every file name read from the list file .\filenames.txt, the directory tree routed at directory C:\SVN\nlbavwdocsvn\rep_doc_erpln\trunk\ERPLN is searched for matching files recursively, and the respective modify date/time stamp is gathered. Due to its format, a simple greater-than (GTR) comparison can be done do determine whether or not it is a later point of time than a cached one; if the criterion is fulfilled, the cache is updated accordingly.
The upper-case REM and ECHO commands constitute placeholders only for the real action to be performed on the files. Extend the script there as you like. Variable !LASTFILE! holds the full path to each encountered file.
So here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "LOCATION=C:\SVN\nlbavwdocsvn\rep_doc_erpln\trunk\ERPLN"
set "FILELIST=.\filenames.txt"
set "WMICPROP=LastModified" & rem // {CreationDate | LastAccessed | LastModified}
pushd "%LOCATION%" || exit /B 1
for /F "usebackq eol=| delims=" %%L in ("%FILELIST%") do (
set "LASTFILE="
set "LASTFAGE=00000000000000.000000+000"
for /F "eol=| delims=" %%F in ('dir /B /S /A:-D "%%~L"') do (
set "FILE=%%F"
setlocal EnableDelayedExpansion
set "FILE=!FILE:\=\\!"
for /F "tokens=2 delims==" %%J in ('
2^> nul wmic DataFile WHERE ^(Name^="!FILE!"^) GET %WMICPROP% /VALUE ^|^| ^
2^> nul wmic DataFile WHERE Name^="!FILE!" GET %WMICPROP% /VALUE
') do for /F %%I in ("%%J") do (
endlocal
set "FAGE=%%I"
setlocal EnableDelayedExpansion
if !FAGE! GTR !LASTFAGE! (
endlocal
set "LASTFILE=%%F"
set "LASTFAGE=%%I"
setlocal EnableDelayedExpansion
)
)
endlocal
)
if defined LASTFILE (
setlocal EnableDelayedExpansion
REM Do whatever you want with the file here...
ECHO Newest file: "!LASTFILE!"
endlocal
)
)
popd
endlocal
exit /B
I'm trying to set the value of a variable that is inside a SETLOCAL, inside a FOR loop and inside an IF statement. However, it never seems to work. I tried using a SET statement at the ENDLOCAL statement, but that doesn't seem to actually set anything. Echoing the variable after that only echos the original set value of 0.
#ECHO off
SET pathsource=K:
SET sourcefile=0
SET counter=0
SETLOCAL enableextensions enabledelayedexpansion
REM Get the newest pptx file in the specified directory. Get the filename and last modified timestamp
FOR /f "tokens=1-3,5*" %%a IN ('dir ^"%pathsource%\*.pptx^" /a-d-h-s /o-d /tw ^| find /i ^".pptx^"') DO (
REM echo !counter!
REM only get the first row by using a counter
IF !counter! EQU 0 (
REM variables are: a-date, b-time, c-am/pm, d&e-filename
ECHO %%a %%b %%c %%d %%e
SET sourcefile=%%d %%e
)
SET /A counter+=1
)
ENDLOCAL & (
SET "sourcefile=%sourcefile%"
)
ECHO %sourcefile%
REM do other stuff with the %sourcefile% variable after this
Because you assign the values within the Set/End Local block, any changes made within this are discarded once on the ENDLOCAL command is reached.
Just move your SETLOCAL and ENDLOCAL commands to the very top and bottom of your script respectively. This would make all assignments made within the entirety of script stick throughout.
Additionally, you don't really need the counter variable. You can just jump out of the loop after the first file is processed:
#ECHO off
REM delayed expansion not needed for the portion shown
SETLOCAL enableextensions
SET pathsource=K:
SET sourcefile=0
REM Get the newest pptx file in the specified directory. Get the filename and last modified timestamp
FOR /f "tokens=1-3,5*" %%a IN ('dir ^"%pathsource%\*.pptx^" /a-d-h-s /o-d /tw ^| find /i ^".pptx^"') DO (
REM variables are: a-date, b-time, c-am/pm, d&e-filename
ECHO %%a %%b %%c %%d %%e
SET sourcefile=%%d %%e
REM We only care about the first row
GOTO EndLoop
)
:EndLoop
ECHO %sourcefile%
REM do other stuff with the %sourcefile% variable after this
ENDLOCAL
One last thing is you can simply your FOR syntax a bit by using native DIR and FOR variable commands:
#ECHO off
REM delayed expansion not needed for the portion shown
SETLOCAL enableextensions
SET pathsource=K:
SET sourcefile=0
REM Get the newest pptx file in the specified directory. Get the filename and last modified timestamp
CD "%pathsource%\"
FOR /f "usebackq tokens=* delims=" %%a IN (`dir "%pathsource%\*.pptx" /a-d-h-s /o-d /tw /b`) DO (
REM Use For loop variables.
REM Print the date/time and file name.
ECHO %%~ta %%~a
SET sourcefile=%%~a
REM We only care about the first row
GOTO EndLoop
)
:EndLoop
ECHO %sourcefile%
REM do other stuff with the %sourcefile% variable after this
ENDLOCAL
If all you want is the name and timestamp of the newest PPTX file, your script is a lot more complicated than it needs to be. Simply scrape dir "path\*.pptx" /b /o:-d then break out of the for /f loop after the first line.
#ECHO off
setlocal
SET "pathsource=K:"
REM Get the newest pptx file in the specified directory. Get the filename and last modified timestamp
pushd "%pathsource%"
FOR /f "delims=" %%a IN ('dir "*.pptx" /b /o:-d') DO (
set "sourcefile=%%~nxa"
set "timestamp=%%~ta"
goto break
)
:break
ECHO %sourcefile%
echo %timestamp%
REM do other stuff with the %sourcefile% variable after this
I've a batch script in which I loop trough all the filenames in a folder and then I invoke a command in order to remove the extension from the filename. I'd like to store at every iteration the filename (without extension) in a variable named result for later reuse.
The RemoveExtension function works fine. However I'm not able to retrieve the result and store it in the _result variable. When I print it, it's always empty. Thanks for your help!
#echo OFF
SETLOCAL EnableDelayedExpansion
set "_result="
for /f "delims=" %%i in ('dir "%~1\*.txt" /b') DO (
echo.filepath: "%~1\%%i"
call :RemoveExtension "%%i"
echo._result: "%_result%" // The "_result" variable is always EMPTY ""
)
goto :eof
:RemoveExtension
SETLOCAL
REM echo "%~1"
set "filename=%~1"
:loop
if "%filename:~-1%" NEQ "." (
set "filename=%filename:~0,-1%
goto :loop
)
set "filename=%filename:~0,-1%"
echo "%filename%"
ENDLOCAL & set "_result=%filename%"
goto :eof
Try this...much simpler way to remove the extension. Because you are changing _result inside a FOR loop, you need to access it using ! instead of %.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "_result="
FOR /f "delims=" %%i IN ('dir "%~1\*.txt" /b') DO (
ECHO.filepath: "%~1\%%i"
SET _result=%%~ni
ECHO._result: "!_result!"
)