I have a list in text file
abc
abc_1
abc_2
cbd
cbd_1
cbd_2
And theres alot more to the lines as well like dates and other randomness. What I want to do is for a script to like for each file, but when it looks for a file it will compare to another text for for that same file name. And then if it doesn't find it, write it to that text and continue with a full search. The part of the script I am stuck on...says do was unexpected at this time.
for /f "delims=" %%a in (C:\conflicts\vids.txt) DO for /f "tokens=1 delim=_" %%i ("%%a") do (
find %%i in (C:\conflicts\exists.txt) do (
if exist goto :end
echo %%i >> C:\conflicts\exists.txt
) else if exist \\networklocation\%%i.mpg (
ECHO FOUND %%i IN NETWORKLOCATION >> C:\conflicts\vids1.txt
... DO for /f "tokens=1 delim=_" %%i ("%%a") do
missing 'IN' probably generating your 'unexpected do'
... if exist goto :end
If exist what goto end?
if exist looks for a match. This would look for a file named 'goto' and if found would execute `:end'
You've only posted a partial statement - there are missing round-brackets and who-knows-what else.
Please give examples to demonstrate what you are trying to achieve. Your torrent of pronouns is unfortunately, indecipherable.
try this out .. maybe it does what you want ..
for /f "tokens=*" %%c in (your text file here) do (
cd\
for / r %%f in (*) do (
if %%c==%%~nf echo file name match found %%f
pause
)
)
after the if statement you can put any command you wish depending on what you wanna do with the results ....
hope this helps out ..
Related
I am writing a script to satisfy following conditions:
I have a csv file with 2 columns, I need to read the file line by line and take values from second column only , lets say values are test1, test2 etc.
Once I have the values, I want to search a windows directory as test1.log etc.
If I get a match against any entry during the search, I want to copy the item to another windows dir.
I have the input file and the search directory is constant as well as file name format is constant.
I used following script:
#ECHO OFF
FOR /f "tokens=2 delims=," %%b IN (test.txt) DO^
( dir *%b%*.log IN "D:\test" if
(*%b%*.log = "" echo "file not found" )
Else DO xcopy "D:\test\"*%b%*.log C:\dest )
But, I can't run the code.
Please help.
Here's an example to help you:
#Echo Off
Set "InFile=test.txt"
Set "ChkDir=D:\test"
Set "SavDir=C:\dest"
If Not Exist "%ChkDir%\" GoTo :EOF
If /I Not "%CD%"=="%SavDir%" (CD /D "%SavDir%" 2>Nul||GoTo :EOF)
For /F "UseBackQ Tokens=2 Delims=," %%A In ("%InFile%"
) Do If Exist "%ChkDir%\*%%~A*.log" (Copy /Y "%ChkDir%\*%%~A*.log">Nul 2>&1
) Else Echo "%ChkDir%\*%%~A*.log not found"
Pause
Still a bit unclear, maybe this works:
#ECHO OFF
FOR /f "tokens=2 delims=," %%b IN (test.txt
) DO If not exist "D:\test\*%%b*.log" (
echo "file "D:\test\*%%b*.log" not found"
) Else (
xcopy "D:\test\*%%b*.log" C:\dest
)
I have about 100 .xml files, but its filenames are not self-explanatory. Therefore I would like to look for a specific word and extend the respecting file with this word.
Thanks to some other helpful entries, I could partially figure out how to do this, but somehow the files are not renamed.
Has anybody an idea what is wrong with my code? Thank you in advance.
chcp 1252
SET sourcedir=P:\path
FOR /f "tokens=1 delims=." %%a IN ('findstr /c:"3256" "%sourcedir%\*.xml"') DO (
ECHO (REN "%%a.xml" "%%a_3256.xml")
)
pause
chcp 1252
SET "sourcedir=P:\path"
FOR /f "delims=" %%a IN ('findstr /m /c:"3256" "%sourcedir%\*.xml"') DO (
ECHO REN "%%a" "%%~na_3256%%~xa"
)
pause
In your code you are using %%a for both arguments to ren command, but the second argument must only contain the name and extension, without path. You can solve it using %%~na, that is, the file name (without path or extension) of the file being referenced by %%a (%%~xa is the extension)
Also, if the string is found more than once in any file, your code will try to rename the file twice (or more). It is better to use the /m switch in findstr to only retrieve the list of files.
Rename operations are only echoed to console. If the output is right, remove the echo command.
Dang it MC ND, right after I did some frankenstein scripting, coming up with:
After trial and error along with this great website, I managed to come up with (giving credit to DOS Batch : remove characters from string in a FOR loop )
#ECHO OFF
set pathd=C:\SomePathHere
setlocal enabledelayedexpansion
for /f %%a in ('findstr /ic:SearchString %pathd%\*.xml') do (
set str=%%a
set str=!str:^:=!
echo !str!
)
Here is my another challenge. When I pass the file name into for loop, I want to get a specific word from the file name and need to pass that as a parameter as well.
For eg:
When there are 5 files in a folder say
Encrypted_abc_1.xml
Encrypted_xyz_2.xml
Encrypted_hij_3.xml
Encrypted_klm_4.xml
Encrypted_nop_5.xml
I want to pass the file name Encrypted_abc_1.xml and abc into the for loop. I got the answer for both in seperate for loops however how can i merge into one single for loop
First for loop will fetch the word
for /f "tokens=2 delims=_." %%a in (Encrypted_*.xml /b) do (
Echo %%a
Another for loop will get the full file name
for /R "%Path%" %%f in (Encrypted_*.xml) do (
Echo %%f
I want to merge in single for loop. Can you please help me?
Here you go:
#echo off
setlocal
for /f "tokens=*" %%a in ('dir /b %1\Encrypted*.xml') do (
echo %%a & for /f "tokens=2 delims=_" %%b in ("%%a") do (
echo %%b
)
)
Make sure you pass in the path to the script.
Here is the output I get
C:\temp>testfilenameparse.cmd c:\path
Encrypted_abc_1.xml
abc
Encrypted_samp_3.xml
samp
Encrypted_xyz_2.xml
xyz
I'm using a tool called Benzin; everyday I have to write a new command for the new files I'm dealing with. I would like to create a text file that has a list of the files I need. Then, look through the list of files and use Benzin on them. Benzin is used as follows:
Benzin R filename.brlyt filename.xmlyt
I'd like the tool to automatically look through the list and then replace "filename.brlyt" with what's written inside the text file and convert the files one by one.
Sadly I don't know how to do this so I thought of doing another thing, I just simply place the files inside a folder and do this command:
FOR /D %%A IN ("Files\*.brlyt") Do Benzin.exe R %%A %%A.xmlyt
pause
cls
rename *.brlyt.xmlyt *.xmlyt
pause
That is giving file not found error, can anyone see what's wrong?
Ok, so I've found the code
#echo off
for /f %%A in (files.txt) do (
echo converting
echo %%A
benzin r %%A coverted/%%A.xmlyt
pause
cls
)
pause
rename *.brlyt.xmlyt *.xmlyt
pause
How do I make another batch that writes the list of files in a directory into the text files.txt? Here's what I'm trying but it is only showing me "\files\" inside the text file.
#echo off
FOR /D %%A in (\files\) Do (
echo writing %%A into files.txt
echo %%A > files.txt
)
pause
Starting with:
FOR /F "tokens=*" %%A IN ('TYPE test_list.txt') DO ECHO %%A
PAUSE
Now, looking at, we see: http://www.robvanderwoude.com/files/servers_nt.txt
FOR /F "tokens=*" %%A IN ('TYPE %~dpn0.srv ^| FIND /V ";" ^| SORT') DO CALL :ChkSrv %%A
GOTO End
I would use/re-use that for your code.
I try to create a batch file to automaticaly rename the files contained in a folder, from this structure: A12345678.textornumbers.textornumbers.txt to a more simple one: A12345678.txt
I tried something like this:
#echo off
ECHO Renaming files
Pause
:begin
ECHO Renaming txt files
for /r %%x in (A*.*.*.txt) do (REN "%%x" A*.txt)
ECHO Renaming finished
:end
pause
done
It does not return any error, but it does nothing...
EDITED:
Ok, the problem could be seen in other way: i want to rename the file remaining the first 9 characters and the extension.
I saw a similar question here:
Deleting characters from filename
I modified and adjusted to my case to have this:
#echo off
ECHO Renaming files
Pause
:begin
REM setlocal enabledelayedexpansion (the result is the same with and without this line)
set X=9
ECHO Renaming files
for /r %%f in (*.txt) do if %%f neq %~nx0 (
set "filename=%%~nf"
set "filename=!filename:~%X%,-%X%!"
ren "%%f" "!filename!%%~xf")
ECHO Done
ECHO Processing finished
:end
pause
done
But the result is this:
Blockquote
!filename!.txt
This is for the first image in the directoy, and errors (Such file already exist) for the others.
EDITED 2:
Thanks to the replies and other information i found on internet, here is the solution i had: to remove the last characters of the filename, because i want to remain ever the first 9 characters:
#echo off
ECHO renaming files
ECHO.
Pause
:begin
set ext=QUB
set num=17
FOR /f "tokens=*" %%f in ('dir /b /a *.%ext%') do call :lab %%f
ECHO Done
pause
Exit
:lab
set original=
set original=%*
set newname=
call set newname=%%original:.%ext%=%%
call set newname=%%newname:~0,-%num%%%.%ext%
if "%newname%"==".%ext%" (goto :eof)
ren "%original%" "%newname%"
ECHO %newname%
goto :eof
This is not my code, but the solution i used from others (by Carlitos.dll). I hope it could help to others with similar problems. Thanks for your ideas and help!
The script does do something, but nothing useful - It renames each file to it's original name :-(
It all has to do with the rules for how REN works with wildcards. I had never seen any proper explanation of how REN works posted anywhere. So a few weeks ago I did extensive experiments and developed a set of rules that explain all the behavior I observed.
You can find my results at How does the Windows RENAME command interpret wildcards? on the StackExchange SuperUser site.
Your problem is easily solved by replacing * with many ? in your target name. Just make sure the number of ? is greater than or equal to the max leading name length that you will process. You also don't need to specify the leading A if you are not changing the value.
for /r %%x in (A*.*.*.txt) do ren "%%x" ?????????????????????.txt
The above must iterate each file. It may be a bit faster to iterate the folders only, though I haven't tested
for /r /d %%x in (.) do ren "%%x\A*.*.*.txt" ?????????????????????.txt
It is also possible to parse the name with FOR /F so that you don't have to worry about the number of ?.
for /r %%x in (a*.*.*.txt) do (
for /f "delims=." %%n in ("%%~nx") do ren "%%x" "%%n.txt"
)
EDIT based on revised question
To simply preserve up to the 1st 9 characters and the .txt extension, the solution is even easier, just use 9 ?:
for /r /d %%x in (.) do ren "%%x\*.txt" ?????????.txt
If you want to rename all files, not just .txt files, then
for /r %%F in (*) do ren "%%F" "?????????%%~xF"
You could also do something like this:
for %%x in (*.textornumbers.txt) do (
set nam=%%x
if "!nam!" neq "!nam:.textornumbers=!" ren %%x !nam:.textornumbers=!
)
Using ~-modifiers, you could do the following:
Take just the name from the original full path and name (& extension):
FOR /R %%A IN (A*.*.*.txt) DO (
... %%~nA ...
)
That will turn a D:\path\A*.*.*.txt to just an A*.*.*.
Take just the name from the result of #1:
FOR /R %%A IN (A*.*.*.txt) DO (
FOR %%B IN ("%%~nA") DO (
... %%~nB ...
)
)
That will leave you with A*.*.
Take just the name from #2:
FOR /R %%A IN (A*.*.*.txt) DO (
FOR %%B IN ("%%~nA") DO (
FOR %%C IN ("%%~nB") DO (
... %%~nC ...
)
)
)
which will give you A*.
Extract the extension from the original name and concatenate it with #3. That will be the final name, A*.txt, which you can now supply to the RENAME command:
FOR /R %%A IN (A*.*.*.txt) DO (
FOR %%B IN ("%%~nA") DO (
FOR %%C IN ("%%~nB") DO (
RENAME "%%A" "%%~nC%%~xA"
)
)
)