I have a folder containing a large number of files. A lot of the filenames have '%' and/or '&' characters in them.
e.g. Test&doc.pdf
e.g Test%doc.doc
Is there a quick way I could remove the '%' and '&' characters using a windows batch file, vbscript or something similar?
All help will be greatly appreciated.
Thanks.
Here's how you can do it in batch (in case you're curious). The big limitation is that if you have filenames with more than one percent sign, it won't work because the shell expands it to a variable. I don't know immediately how to fix that.
It starts from whatever directory the script is in, and works recursively over all subdirectories.
#echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%N in (`dir /s /b`) do (
set var=%%~nN
set var=!var:^&= !
set var=!var:%%= !
if not "!var!"=="%%~nN" (
if not exist "%%~dpN!var!%%~xN" (
echo "%%N" --^> "!var!%%~xN"
ren "%%N" "!var!%%~xN"
) else (
echo File "!var!%%~xN" ^(from %%N^) already exists.
)
)
)
E.g., prints output like this:
C:\batch\schar>schar
"C:\batch\schar\Test%doc.doc" --> "Test doc.doc"
"C:\batch\schar\Test%doc.pdf" --> "Test doc.pdf"
File "Test doc.pdf" (from C:\batch\schar\Test&doc.pdf) already exists.
"C:\batch\schar\subdir\FILE%here" --> "FILE here"
#indiv
If someone can produce a batch solution without character limitations, I'll be massively impressed.
Ok, I'll try.
#echo off
setlocal DisableDelayedExpansion
for /f "usebackq delims=" %%N in (`dir /s /b`) do (
set var=%%~nxN
setlocal EnableDelayedExpansion
set "org=!var!"
set "var=!var:&= !"
set "var=!var:%%= !"
if not "!var!"=="!org!" (
if not exist "%%~dpN!var!" (
echo "!org!" --^> "!var!"
ren "!org!" "!var!"
) else (
echo File "!var!" ^(from !org!^) already exists.
)
)
endlocal
)
The trick is, to toggle the delayed expansion, because expanding of for-loop-vars (%%N) should be done without delayed expansion, else you lose the exclamation marks, and got problems with carets.
But to handle and modify the strings you should use delayed expansion.
But why? The answer is to understand the phases of the batch parser.
I tried to explain it here.
how-does-the-windows-command-interpreter-cmd-exe-parse-scripts
I've quickly thrown that together and didn't test it, but this VBScript should do the trick. Tell me if you need fancy stuff like folder recursive replacing etc.
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Your folder here
objStartFolder = "X:\MYFOLDER"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set regEx = New RegExp
'Your pattern here
regEx.Pattern = "[&%]"
Set colFiles = objFolder.Files
For Each objFile in colFiles
objFile.Rename(regEx.Replace(objFile.Name, "")
Next
Related
I have tried to use the answer mentioned from here: [Find Substring in String] (Batch file: Find if substring is in string (not in a file))
I try to adapt the solution mentiones in the commands, so that I have my SearchVal saved inside a variable so this can be changed during runtime.
Minimal example:
set searchVal="cde"
set str1="abcdef"
setlocal enabledelayedexpansion
if not "x!str1:%searchVal%=!"=="x%str1%" echo It contains my subs
endlocal
pause
In my opinion this little batch should display that the strings contains my subs, however nothing is shown and I do not know why as I directly make use of the solution that should be working.
EDIT
Thanks to the commands I found my mistake.
In my current situation I look at files inside a folder and save the filename inside an array while doing a for-loop:
for /f "tokens=1 delims=" %%G in ('pathToFolder\*.properties /b') do (
if not "%%~G:%searchVal%=!"=="%%~G" echo It contains my subs !ID_Properties!
set filename[!ID_Properties!]=%%~G
set /a ID_Properties+=1
)
... where ID_properties is just a counter and searchVal my string I am looking for. Does anyone know how I can use the %%G inside the loop in the correct way so the search works as before?
Your for-loop syntax is not correct it seems like a mixture between executing a dir command and looping through files. I'll stick with the dir command option and using usebackq.
#echo off
setlocal EnableDelayedExpansion
set searchVal=cde
set ID_Properties=0
for /f "usebackq tokens=1 delims=" %%G in (`dir pathToFolder\*.properties /b`) do (
set file=%%G
if not "!file:%searchVal%=!"=="!file!" (
echo It contains my subs !ID_Properties!
set filename[!ID_Properties!]=!file!
set /a ID_Properties+=1
)
)
Filling the array is only done if the file contains your searchVal; don't know if this was/is you intention.
a For loop might be a bit much, depending on what you plan to do with the output. findstr is maybe a shorter option:
findstr /im "cde" *.properties && echo - found || echo not found
and add /s if you require recursive search through subdirectories:
findstr /ims "cde" *.properties && echo - found || echo not found
I'm currently trying to do a script in order to find a sentence in files and replace this line by another one.
I have already made this :
#echo off
setlocal enableextensions disabledelayedexpansion
set "search=#Interceptors({ RuntimeExceptionInterceptor.class })"
set "replace=#Interceptors({ RuntimeExceptionInterceptor.class, ReportInterceptor.class })"
set "textFile=test2.txt"
for /f %%i %%f in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i %%f"
setlocal enabledelayedexpansion
>>"%textFile%" echo(!line:%search%=%replace%!
endlocal
)
But it doesn't work, this script is unable to replace my first line by the one that I want, can you help me please :)?
why not use the sed command with the -i flag? this sounds like what the tool is made for
There were some erros in your code:
the for has only one expressed variable, via "tokens=" you can define more when splitting input at the chars listed with "delims=" See help for
you can't write to the same while still reading it.
the closing parenthesis in your search and replace strings causes the for loop to break - you need to escape them with a ^.
#echo off
setlocal enableextensions Disabledelayedexpansion
:: take care the closing parenthesis has to be escaped with a caret
set "search=#Interceptors({ RuntimeExceptionInterceptor.class }^)"
set "replace=#Interceptors({ RuntimeExceptionInterceptor.class, ReportInterceptor.class }^)"
set "textFile=test2.txt"
set "text_new=test2.new"
( for /f "Delims=" %%i in (
'type "%textFile%" '
) do (
set "line=%%i"
Call echo:%%line:%search%=%replace%%%
)
) > "%text_new%"
type "%text_new%"
Replacing the old file with the new one is left for you.
It's easier to fix the problem using a different approach instead of a batch file. Presumably, you are developing using a Java IDE. Use its global replace facility; e.g. in IntelliJ IDEA, use the option "Replace in path...".
I have two .txt files. One contains numbers, and the other one contains filepaths. I want to combine these two files to a .csv. The combination is based on wether the number (from nrs.txt) is in the string of the filepath (nodups.txt).
Now I have the following code for this:
#setlocal enableextensions enabledelayedexpansion
for /F %a IN (Output\nrs.txt) DO (
SET "nrs=%a"
for /F %b IN (Output\nodups.txt) DO (
SET "pathstring=%b"
SET csvdelim=,
IF NOT x!pathstring:%nrs%=""!==x%pathstring% %nrs%,%pathstring%>>new2017.txt
)
)
#endlocal
However, I keep having the following issues with the code:
The pathstring never seems to get set. (when I run the code without the if statement, The nrs variable gets set but the pathstring is set to %b). I've seen a lot of possible solutions on here already but none seem to work for me (setting variables like !var! and using usebackq).
The IF statement in the second for loop gets the following error message =""!==x%pathstring% was unexpected at this time. The ="" should remove the nr. from the path (if its there). When I replace "" with something else it still does not work.
The file contents are:
File nrs.txt:
12345
12245
16532
nodubs.txt:
C:\tmp\PDF_16532_20170405.pdf
C:\tmp\PDF_1234AB_20170405.pdf
C:\tmp\PDF_12345_20170506.pdf
Desired output:
12345, C:\tmp\PDF_12345_20170506.pdf
16532, C:\tmp\PDF_16532_20170405.pdf
I really hope someone can help me out with this !
This solution use a different approach, based on arrays:
#echo off
setlocal EnableDelayedExpansion
rem Load array from nodubs.txt file
pushd "Output"
for /F "tokens=1,2* delims=_" %%a in (nodubs.txt) do set "nodubs[%%b]=%%a_%%b_%%c"
rem Process nrs.txt file and show output
(for /F %%a in (nrs.txt) do (
if defined nodubs[%%a] echo %%a, !nodubs[%%a]!
)) > new2017.txt
In a batch file for variables need two percent signs.
There is no need to put %%A into a variable, use it directly.
#setlocal enableextensions enabledelayedexpansion
for /F %%a IN (Output\nrs.txt) DO (
findstr /i "_%%a_" Output\nodups.txt >NUL 2>&1 || >>new2017.txt Echo %%a
)
#endlocal
Instead of a second for, I'd use findstr to search for the entry of nrs.txt enclosed in underscores.
if no find use condiotonal execution on failure || to write to the new file.
According to changed preliminaries another answer.
#Echo on
Pushd Output
for /F "tokens=1-3 delims=_" %%A IN (
' findstr /G:nrs.txt nodubs.txt'
) DO >>"..\new2017.txt" Echo %%B, %%A_%%B_%%C
Popd
sample output:
> type ..\new2017.txt
16532, C:\tmp\PDF_16532_20170405.pdf
12345, C:\tmp\PDF_12345_20170506.pdf
Recently I started working and my first task is to write a batch file that automatically changes filenames to filename_date with the original file-ending.
For that you should be able to write paths into a textfile (e.g. paths.txt) and when you start the program, it should take any line (=path->file) from there and rename it.
I got it to work on my PC quiet well but as I gave it to testing they asked to make the use of wildcards Z:\Path\*.* possible.
My current code looks as follows:
#echo off
setlocal EnableDelayedExpansion
cd %~dp0
For /F "tokens=*" %%m in (paths.txt) do (
set path=%%~dpm
set name=%%~nxm
pushd "!path!"
dir
For /r !path! %%f in (!name!) do (
set path=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf
set "name=!name!_"
set "name=!name!!date:~6,4!"
set "name=!name!!date:~3,2!"
set "name=!name!!date:~0,2!"
set "name=!name!!ending!"
copy "!datsave!" "!name!"
del "!datsave!"
cls
popd
)
)
I know that a lot of it is probably easier and more efficient to do, but this is my first batch project and I am quiet happy except for the wildcard problem.
So an example would be:
C:\Some\Path\*.*
This line would be in paths.txt.
With the splitting
set path=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf
I get the following:
path: C:\Some\Path
name: C:\Some\Path
ending: -empty-
datsave: C:\Some\Path
because name is set to the Path at the start of the first FOR-Loop. But that seems to be working if I do not use wildcards.
Now the question: Why does this happen and how do I get rid of it? Or do I just use the wrong type of wildcards?
Again: This is my first time I work with batch, so it might be something simple ;)
Ok, I figured out 2 problems and now it works
set name=%%~nxm evaluates the wildcard. Even if name is *.txt it will return bar.txt.
I replaced that by a basename computation instead: set name=!name:*\=! done enough times (not very subtle but hey batch files forces us to do such things) which preserves the wildcard
The other problem is the for /R loop: after pushd, the argument needs to be . or it won't be scanned.
Last minor one: use rename instead of copy plus delete. It preserves file time and is very fast. Copying then deleting a large file can take a long time.
#echo off
set DEPTH=20
setlocal EnableDelayedExpansion
cd %~dp0
For /F %%m in (paths.txt) do (
set pth=%%~dpm
set z=%%m
set name=!z!
rem brutal basename. We cannot break the inner loop or
rem it would break the upper loop too
for /L %%I in (1,1,%DEPTH%) do set name=!name:*\=!
rem but we can check if it is really a basename
set chkname=!name:*\=!
if not !chkname!==!name! ( echo please increase DEPTH value
pause
exit /B)
rem set name=%%~nxm
pushd "!pth!"
For /r . %%f in (!name!) do (
set pth=%%~dpf
set name=%%~nf
set ending=%%~xf
set datsave=%%~nxf
set "name=!name!_!date:~6,4!!date:~3,2!!date:~0,2!!ending!
echo renaming "!datsave!" to "!name!"
rem ren "!datsave!" "!name!"
popd
)
)
paths.txt contains just a line C:\full\path\to\test\*.txt
my test directory contains 2 text files and 1 other file
output:
renaming "bar.txt" to "bar_20160812.txt"
renaming "foo.txt" to "foo_20160812.txt"
(just uncomment the ren line to get the job done)
Weeeeell First of all thanks again to #Jean-François Fabre and #aschipfl for their patience with me :)
After the hint with the second batch file I had to test a few things as not everything worked as fine, but now everything works great!
Code of the Main file:
#echo off
setlocal EnableDelayedExpansion
cd %~dp0
set DEPTH=20
For /F %%m in (paths.txt) do (
pause
set pth=%%~dpm
REM pushd !pth!
REM set origpth=!cd!
REM popd
set z=%%m
set name=!z!
For /L %%i in (1,1,%DEPTH%) do set
name=!name:*\=!
set chkname=!name:*\=!
if not !chkname!==!name! ( echo depth to small
pause
exit /B)
rem set name=%%~nxm
pushd "!pth!"
For /r . %%f in (!name!) do (
pushd %~dp0
call renamefiles.bat %%f REM "!origpth!"
popd
)
)
And the code of the sub-file:
#echo off
REM set pth=%~dp1
REM set origpth=%2
REM set origpth=%origpath:"=%\
REM If !pth!==%origpth% (
set path=%~dp1
set name=%~n1
set ending=%~x1
set datsave=%~nx1
pushd !path!
set "name=!name!_!date:~6,4!!date:~3,2!!date:~0,2!!ending!"
pause
echo renaming "!datsave!" to "!name!"
rem "!datsave!" "!name!"
cls
popd
REM )
EDIT: After testing around a bit I figured, that subfolders are included as well! I put extra code to both codes marked with REM and two extra spaces. Take out those REM's and the programm will not longer include subfolders when renaming :)
I have a text file with the names of computer names and corresponding static i.p. addresses in the following format.
COMPUTER NAME:PC ADDRESS=154.100.1.1 MASK=255.255.254.0
COMPUTER NAME:PC2 ADDRESS=100.100.1.1 MASK=255.255.254.0
I would like to take the values from each line and put them as variables in a batch file for use later. Is this possible? The overall goal is to have the values from this easily edited text file to be used in netsh commands in another batch file.
I've looked around and found ways to take lines of a text file and place them in one variable using the snippet below. However, I do not know how to create multiple variables from one line. If someone could help me with this I'd greatly appreciate it!
#echo o
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in (D:\COMP_T.txt) do (
set "comp!Counter!=%%x"
set /a Counter+=1
)
This should work:
#echo off
setlocal EnableDelayedExpansion
set "Count=1"
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%A in (C:\File.txt) do (
set "%%A[!Count!]=%%C"
set "%%D[!Count!]=%%E"
set "%%F[!Count!]=%%G"
set /a "Count+=1"
)
:: Call other batch script here.
endlocal
Example Output:
COMPUTER[1]=PC
COMPUTER[2]=PC2
ADDRESS[1]=154.100.1.1
ADDRESS[2]=100.100.1.1
MASK[1]=255.255.254.0
MASK[2]=255.255.254.0
Here is a solution that avoids the need for delayed expansion. It uses FINDSTR to insert a line number followed by : at the beginning of each line. The search string of "^" is guaranteed to match every line in the file.
The only other issue is to set TOKENS and DELIMS to parse the line properly.
#echo off
setlocal
for /f "tokens=1,4,6,8 delims=:= " %%A in ('findstr /n "^" "d:\comp_t.txt"') do (
set "comp%%A=%%B"
set "addr%%A=%%C"
set "mask%%A=%%D"
set "counter=%%A"
)
To use the set of variables in another batch file, line by line, just parse the lines as done in other answers here, and call the other batch file with the metavariables.
#echo off
for /f "tokens=1,2,3,4,5,6,7 delims==: " %%a in ('type "File.txt" ') do (
echo "computer_name=%%c"
echo "address=%%e"
echo "mask=%%g"
Call "batch script" "%%c" "%%e" "%%g"
)