I'm trying to write a code that does the following. I have some files in a directory with specific extensions. I have made a vector that contains all of them. Now I will want to rename each file to something else depending on their extension. So for that I'm trying to parce the created vector with a for loop in which I check for each element extension.
For now I won't rename it just echo it on the screen if the file with the .elf extension is found. I wrote this code but I get no echo as in there would be no .elf file in my directory. Please help me correct this. Thanks.
#echo off
setlocal enabledelayedexpansion
cd C:\Users\uidr0938\Desktop\Copy
set path=C:\Users\uidr0938\Desktop\Copy
set /a index=0
for /r %%i in (*) do (
set value[!index!]=%%i
set /a index+=1
)
set /a limit=%index%-2
for /l %%a in (0;1;%limit%) do (
if !value[%%a]! equ *.elf (
echo !value[%%a]!
)
)
endlocal
try with :
....
for /l %%a in (0;1;%limit%) do (
if "!value[%%a]:~-4!" equ ".elf" (
echo !value[%%a]!
)
)
when comparing string you cannot use wildcards.Here you can see some examples about batch substrings
Here is a slightly different way of doing it.
#IF NOT EXIST "%USERPROFILE%\Desktop\Copy\" #EXIT/B
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_path=%USERPROFILE%\Desktop\Copy"
SET "_index=0"
FOR /F "DELIMS=" %%A IN ('WHERE/R "%_path%" * 2^>NUL') DO (SET/A "_index+=1"
SET "_value[!_index!]=%%A")
IF %_index% EQU 0 EXIT/B
FOR /F "TOKENS=1* DELIMS==" %%A IN ('SET _value['
) DO IF /I "%%~xB"==".elf" ECHO %%B
PAUSE
Related
How can we split string using windows bat script?
for below .bat code snippet
#echo off & setlocal EnableDelayedExpansion
set j=0
for /f "delims=""" %%i in (config.ini) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a!
(echo !a!|findstr "^#">nul 2>nul && (
rem mkdir !a!
) || (
echo +)
rem for /f "tokens=2" %%k in(config.ini) do echo %%k
)
)
pause
below config file
Q
What's wrong when I del rem at the begin of rem for /f "tokens=2" %%k in(config.ini) do echo %%k
How can I get the /path/to/case and value as a pair?
for /f xxxx in (testconfig.ini) do (set a=/path/to/case1 set b=vaule1)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q43407067.txt"
set j=0
for /f "delims=""" %%i in (%filename1%) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a! SHOULD BE EQUAL TO %%i
(echo !a!|findstr "^#">nul 2>nul && (
echo mkdir !a!
) || (
echo +)
for /f "tokens=2" %%k IN ("%%i") do echo "%%k"
for /f "tokens=1,2" %%j IN ("%%i") do echo "%%j" and "%%k"
)
)
ECHO ----------------------------
SET con
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q43407067.txt containing your data for my testing.
(These are setting that suit my system)
SO - to address your problems:
because the ) on that line closes the ( on the previous. The ) on that line closes the ( on the one prior. (I changed the rem to an echo so that the code would produce something visible) The first ( on the (echo !a! line is closed by the ) on the line following the (now) two for /f commands. and the ( on the for..%%i..do( is closed by the final ) before the echo -----
You can't delete that ) because it's participating in a parenthesis-pair.
You need a space between the in and the (.
I've shown a way. See for /?|more from the prompt for documentation (or many articles here on SO)
In your code, !a! is the same as %%i - so I've no idea why you are conducting all the gymnastics - doubtless to present a minimal example showing the problem.
Note that since the default delimiters include Space then if any line contains a space in the /path/to/case or value then you'll have to re-engineer the approach.
I' not sure if I understand what exactly it is you need, so what follows may not suit your needs:
#Echo Off
SetLocal EnableDelayedExpansion
Set "n=0"
For /F "Delims=" %%A In (testConfig.ini) Do (Set "_=%%A"
If "!_:~,1!"=="#" (Set/A "n+=1", "i=0"
Echo=MD %%A
Set "con[!n!]!i!=%%A") Else (For /F "Tokens=1-2" %%B In ('Echo=%%A'
) Do (Set/A "i+=1"
Set "con[!n!]!i!=%%B"&&Set/A "i+=1"&&Set "con[!n!]!i!=%%C")))
Set con[
Timeout -1
GoTo :EOF
remove Echo= on line 6 if you are happy with the output and really want to create those directories
I want to fetch two sub string from my file name in order to create Folder String.
My file name is "SM-SM-ABC_ab12 cd34_AA 11_abc123.txt"
here "ab12 cd34" is 1st folder and "AA 11" is 2nd folder
I have written a code but After adding #Compo code I ma not able to move file to directory. I want to move multiple files to respective folders.
Can some one help whats wrong?
#Echo Off
set Path1= d:\A
:: SDate=DAYMONTHYEAR FORMAT of Systemdate
echo %Path1%
set SDate=%date:~7,2%%date:~4,2%%date:~10,4%
echo %SDate%
::Variable for folder path
Pushd %Path1%
for %%i in (*.*) do SET "FPath=%%~ni"
For /F "Tokens=2-3 Delims=_" %%A In ("%FPath%") Do (
Set "FoldOne=%%A"
Set "FoldTwo=%%B"
if not exist "%Path1%\%FoldOne%\%FoldOne%\%SDate%" (
mkdir "%Path1%\%FoldOne%\%FoldOne%\%SDate%" )
move %Path1%\* "%Path1%\%FoldOne%\%FoldTwo%\%SDate%\"
echo test %Path1%
echo test %FPath%
)
GoTo :EOF
Is this what you are trying to achieve?
#Echo Off
Set "FPath=SM-SM-ABC_ab12cd34_AA11_abc123.txt"
For /F "Tokens=2-3 Delims=_" %%A In ("%FPath%") Do (
Set "FoldOne=%%A"
Set "FoldTwo=%%B")
Echo(%%FoldOne%%=%FoldOne%
Echo(%%FoldTwo%%=%FoldTwo%
Timeout -1
GoTo :EOF
[Edit /]The following code may provide you with a solution for your updated requirements:
#Echo Off
Set "Path1=D:\A"
If /I Not "%CD%"=="%Path1%" Pushd "%Path1%" 2>Nul || Exit/B
For /F "EOL=L" %%A In ('WMIC OS GET LocalDateTime') Do For %%B In (%%~nA
) Do Set "SDate=%%B"
Set "SDate=%SDate:~6,2%%SDate:~4,2%%SDate:~,4%"
For %%A In ("*_*_*_*.*") Do Call :Sub "%%A"
Timeout -1
GoTo :EOF
:Sub
For /F "Tokens=2-3 Delims=_" %%A In (%1) Do If Not "%%A"=="" If Not "%%B"=="" (
If Not Exist "%%A\%%B\%SDate%\" MD "%%A\%%B\%SDate%"
Move %1 "%%A\%%B\%SDate%")
I have an issue with reading lines from a *.txt file in batch script to get a list of files.
If my file contain something like
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme!!!.txt
2 errors.
and my batch is
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set count_to_sync=-1
for /f "tokens=*" %%i in (bubu.txt) do (
set line=%%i
echo %%i
if "!line:~0,9!" == "MD5 FAIL:" (
set /A count_to_sync+=1
set list[!count_to_sync!]=!line:~10!
)
if "!line:~0,20!" == "File does not exist:" (
set list[!count_to_sync!]=!line:~21!
set /A count_to_sync+=1
)
)
IF "%count_to_sync%" == "-1" (
ECHO Nothing to sync
) ELSE (
ECHO Files to sync
for /F "tokens=2 delims==" %%s in ('set list[') do (
echo %%s
)
)
The output is
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme.txt
2 errors.
Files to sync
release\devpath\readme.txt
release\mainline\readme.txt
and the '!!!' from second line is missing.
I know that if I remove SETLOCAL ENABLEDELAYEDEXPANSION from batch the output will be
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme!!!.txt
2 errors.
First part is OK, but the extraction will not work because delayed expansion is disabled.
How I can get the correct output?
Thank you
UPDATE
The input file with all types of lines
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme!!!.txt
MD5 FAIL: exf.exe
2 errors.
UPDATE
I need this script to sync changed files based on the output of 'exf.exe' used to check the integrity of folder based on md5 checksum
When the specifications of a problem are not described, but based on examples, we can make assumptions that may or may not be correct. My assumption is that you want the last token of the lines in your text file, so this is a possible (and much simpler) solution:
EDIT: I changed my original method for a simpler one.
#echo off
setlocal
set "msg=Nothing to sync"
(for /F "tokens=3,5" %%a in (bubu.txt) do (
set "msg=Files to sync"
if "%%b" neq "" (echo %%b) else echo %%a
)) > list.txt
echo %msg%
type list.txt
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q35477317.txt"
ECHO Files to sync
FOR /f "usebackqtokens=4*delims=: " %%a IN ("%filename1%") DO ECHO(%%b
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q35477317.txt containing your data for my testing.
It's not clear why you've taken your approach - is there something you haven't told us?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q35477317.txt"
ECHO Files to sync
FOR /f "usebackqtokens=1*delims=:" %%a IN ("%filename1%") DO (
FOR /f "tokens=*" %%c IN ("%%b") DO ECHO(%%c
)
GOTO :EOF
Easily fixed once we get the full story...
Maybe you need something like that:
#ECHO OFF
rem SETLOCAL ENABLEDELAYEDEXPANSION
set count_to_sync=-1
for /f "tokens=*" %%i in (bubu.txt) do (
set line=%%i
echo %%i
SETLOCAL ENABLEDELAYEDEXPANSION
if "!line:~0,9!" == "MD5 FAIL:" (
set /A count_to_sync+=1
set list[!count_to_sync!]=!line:~10!
)
if "!line:~0,20!" == "File does not exist:" (
set list[!count_to_sync!]=!line:~21!
set /A count_to_sync+=1
)
ENDLOCAL
)
IF "%count_to_sync%" == "-1" (
ECHO Nothing to sync
) ELSE (
ECHO Files to sync
for /F "tokens=2 delims==" %%s in ('set list[') do (
echo %%s
)
)
I've been struggling with this for several days... there is one folder with a lot txt files with random names that are generated from server timestamps, but content of files must not be identical for two files in that folder! any ideas? my only option is using windows batch
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR %%a IN (*.*) DO (
FOR %%c IN (*.*) DO IF /i "%%~nxa" lss "%%~nxc" IF "%%~za"=="%%~zc" (
FC "%%a" "%%c" >NUL
IF NOT ERRORLEVEL 1 ECHO "%%a" and "%%c" are identical
)
)
GOTO :EOF
You would need to change the setting of sourcedir and of the filemask *.* to suit your circumstances.
Revision for only-one-mention-of-a-duplicate-file
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR %%a IN (*.*) DO (
SET "reported="
FOR %%c IN (*.*) DO IF NOT DEFINED reported IF /i "%%~nxa" lss "%%~nxc" IF "%%~za"=="%%~zc" (
FC /b "%%a" "%%c" >NUL
IF NOT ERRORLEVEL 1 ECHO "%%a" and "%%c" are identical&SET reported=Y
)
)
GOTO :EOF
I've also added /b to fc to allow for non-text files.
The solution below process the list of file names just once, so it should run faster.
#echo off
setlocal EnableDelayedExpansion
for %%a in (*.txt) do (
if not defined size[%%~Za] (
set size[%%~Za]="%%a"
) else (
set newName="%%a"
for %%b in (!size[%%~Za]!) do (
fc "%%a" %%b >NUL
if not errorlevel 1 (
echo "%%a" and %%b are identical
set "newName="
)
)
if defined newName set "size[%%~Za]=!size[%%~Za]! !newName!"
)
)
If two files are identical, the name of the second one is not saved in the lists, so it is not compared again vs. other same size files. If no more than two files may be identical, then this method could be modified so the name of the first file be also removed from the lists (below the echo ... are identical command), so the method be even faster.
If you may download a third party program that calculate the MD5 checksum, then it may be used to check if two files are identical instead of fc command as foxidrive suggested. This would be faster because the MD5 checksum of each file would be calculated just once and stored in another array (with the file name as index).
As this code uses certutil, this will work only for windows Vista or later versions of the OS. This will check for duplicates in files of the same size and will only read each involved file only once.
#echo off
rem Configure environment
setlocal enableextensions disabledelayedexpansion
rem Where to search for files
set "folder=%cd%"
rem We need a temporary file to hold the size sorted list of files
set "tempFile=%temp%\%~nx0.%random%%random%%random%.tmp"
rem Change to target folder and work from here
pushd "%folder%"
rem Retrieve the list of files with its size and set a environment variable
rem named as the size of the file. The value of this variable will hold the
rem number of files with this size
(for /f "delims=" %%a in ('dir /a-d /b /os *') do (
echo \%%~za\%%a\
set /a "sz_%%~za+=1"
)) >"%tempFile%"
rem Retrieve the list of sizes that happens more than one time
for /f "tokens=2,3 delims=_=" %%a in ('set sz_') do if %%b gtr 1 if %%a gtr 0 (
rem Retrive the list of files with the indicated size
setlocal
for /f "tokens=1,2 delims=\" %%c in ('findstr /l /b /c:"\%%a\\" "%tempFile%"') do (
set "hash="
for /f "skip=1 delims=" %%e in ('certutil -hashfile "%%d"') do if not defined hash (
rem For each file, compute its hash. This hash is used as a variable name.
rem If the variable is defined, a previous file has the same size and hash
rem so it is a duplicate
set "hash=1"
if defined "%%e" (
<nul set /p ".=%%d = "
setlocal enabledelayedexpansion
echo(!"%%e"!
endlocal
) else (
rem Store the name of the file in a variable named as the hash of the file
set ""%%e"=%%d"
)
)
)
endlocal
rem This inner setlocal/endlocal ensures there is no collision between hashes for
rem files with different sizes
)
rem Cleanup
popd
del /q "%tempFile%" >nul 2>nul
endlocal
edited For a simplified version with no temporary file (the list is created in memory) while still reading only the needed files only once each file, AND as demanded a more readable output
edited again to correct a problem with the output of different groups of duplicated for the same file size
#echo off
setlocal enableextensions disabledelayedexpansion
set "folder=%~1"
if not defined folder set "folder=%cd%"
pushd "%folder%"
for /f "delims=" %%a in ('dir /a-d /b /os *') do (
set /a "sz_%%~za+=1"
setlocal enabledelayedexpansion
for /f "delims=" %%b in ("!fl_%%~za! ") do (endlocal & set "fl_%%~za=%%b "%%a"")
)
for /f "tokens=2,3 delims=_=" %%a in ('set sz_') do if %%b gtr 1 (
setlocal & setlocal enabledelayedexpansion
for /f "delims=" %%c in ("!fl_%%a!") do (
endlocal
for %%d in (%%~c) do (
if %%a equ 0 ( set "hash=0" ) else (
set "hash="
for /f "skip=1 delims=" %%e in ('certutil -hashfile "%%~d"') do if not defined hash set "hash=%%e"
)
setlocal enabledelayedexpansion
for /f "delims=" %%e in ("!hash!") do if defined hash_"%%~e" (
for /f "delims=" %%z in ("!hash_"%%~e"!") do (endlocal & set "hash_"%%~e"=%%z"%%~d";")
) else (
endlocal & set "hash_"%%~e"="%%~d"="
)
)
)
for /f "tokens=1,* delims==" %%c in ('set hash_ 2^>nul^|find ";"') do (
set "first=1"
for %%e in (%%d) do if defined first (set "first=" & echo(%%e) else (echo( = %%e)
)
endlocal
)
popd
endlocal
exit /b
I've been working on a batch script (.bat) to change file name to the same folder name 3 levels up but no luck. I can only get it to rename 1 level up. There are multiple files in multiple folders.
Any help would be greatly appreciated.
I don't completely understand the intent of your current code. But the following will rename a given file based on the folder 3 levels up. Note that you can only rename one file with a given extension per folder using this strategy.
#echo off
setlocal
set "file=H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\abc.avi"
for %%A in ("%file%") do for %%B in ("%%~A\..\..\..") do ren "%%~A" "%%~nxB%%~xA"
Note that I include the extension of the folder name because folder names can include dots.
EDIT
Based on OP's comments, I believe the following will properly rename all relevant .avi files. The code has been tested, and works in my hands. Simply set the root and files values as appropriate.
#echo off
setlocal
set "root=H:\Techs\Exported Videos\Scenario"
set "files=*.avi"
for %%A in ("%root%\.") do set "root=%%~fA"
for /f "delims=" %%A in ('dir /b /s /a-d "%root%\%files%"') do (
for %%B in ("%%~A\..\..\..") do (
if /i "%%~dpB" equ "%root%\" ren "%%~A" "%%~nxB%%~xA"
)
)
you might try this:
#echo off &setlocal
set /a PathLevel=3
for %%a in (
"H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi"
"H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi"
) do (
call:doit "%%~a"
)
goto:eof
:doit
set "fname=%~1"
set /a cnt=0
for %%a in ("%fname:\=","%") do set /a cnt+=1
set /a cnt-=PathLevel
for %%a in ("%fname:\=","%") do (
set /a cnt-=1
setlocal enabledelayedexpansion
if !cnt! equ 0 (
endlocal
set "nname=%%~a"
) else endlocal
)
echo ren "%fname%" "%nname%%~x1"
goto:eof
output is:
ren "H:\Techs\Exported Videos\Scenario\1-CAS IED\Media player format\A8 West\1-CAS IED.avi" "1-CAS IED.avi"
ren "H:\Techs\Exported Videos\Scenario\2-SAF\Media player format\A8 PTZ\2-SAF.avi" "2-SAF.avi"