How can I rename a file to a file name stripped of all other characters except letters and numbers?
For example:
It's_A Brand (New) #Year,,2016!.txt renamed to ItsABrandNewYear2016.txt
Is it possible to do this with Windows batch?
Save the code below as a Batch file (with .BAT extension):
#if (#CodeSection == #Batch) #then
#echo off
setlocal
set "oldname=It's_A Brand (New) #Year,,2016!.txt"
for %%f in ("%oldname%") do (
for /F %%a in ('cscript //nologo //E:JScript "%~F0" "%%~Nf"') do ECHO ren "%%~f" "%%a%%~Xf"
)
goto :EOF
#end
// JScript section
WScript.Stdout.WriteLine(WScript.Arguments(0).replace(/[\W_]/g,""));
Output:
ren "It's_A Brand (New) #Year,,2016!.txt" "ItsABrandNewYear2016.txt"
#ECHO OFF
SETLOCAL
set "filename=It's_A Br#$^[-+={;}]a.nd & (New) #Year,,2016!.txt"
SET "newname="
FOR %%a IN ("%filename%") DO CALL :strip "%%~na" "%%~xa"
ECHO RENAME "%filename%" to "%newname%"
GOTO :EOF
:strip
SET "string=%~1"
IF NOT DEFINED string SET "newname=%newname:~0,-1%"&GOTO :EOF
:striploop
SET "char=%string:~0,1%"
ECHO "%char%"|FINDSTR /i /r "[a-z] [0-9]" >NUL
IF NOT ERRORLEVEL 1 SET "newname=%newname%%char%"
SET "string=%string:~1%"
IF DEFINED string GOTO striploop
SHIFT
SET "newname=%newname%."
GOTO strip
Reasonably straight-forward. Note that only the last dot in the name (if any) is preserved.
I have written a hybrid batch/JScript utility called JREN.BAT that provides regular expression renaming directly from the command line, without any additional scripting required. It is pure script that runs natively on any Windows machine from XP onward.
The solution is similar to Aacini's, except the find/replace is applied to the entire name, so the regular expression must be a bit more complex to preserve the extension dot.
jren "[^a-z0-9.]|\.(?!txt$)" "" /i /fm "It's_A Brand (New) #Year,,2016!.txt"
The solution can easily be made to remove all non-alphanumeric characters from all file names in the current directory, except it preserves the extension dots.
jren "[^a-z0-9.]|\.(?![^.]+$)" "" /i
Note that JREN is a batch script, so you must use call jren if you use the command within another batch script.
Related
Thanks in Advance.
Using a DOS batch file, I am trying to read a text file that contains several full paths with quotes, separated by a space and write a new file containing one path per line.
For example, I want to turn this file:
"C:\path\filename.doc" "C:\path\filename.doc" "C:\path\filename.doc" "C:\path\filename.doc"
into this:
"C:\path\filename.doc"
"C:\path\filename.doc"
"C:\path\filename.doc"
"C:\path\filename.doc"
I have had some success using the wonderful repl.bat (by dbenham).
type "files.txt" | repl " " "\r\n" x l >"newfile.txt"
But when there are spaces in the filenames or path it breaks a new line in the middle of the path and wrecks it.
Ive tried passing as the search variable into repl using the escape character ^, i.e. repl "^" ^"" and other ways with no joy.
At the end of the day, I simply need to move all the files into another directory, and so was going to then pass the resulting text file to another bulk delete batch file for processing, but perhaps there is a better way im missing ?
This has a limitation in the length of the line, of around 8 KB.
Less than that and it will move the files to your new folder.
#echo off
for /f "usebackq delims=" %%a in ("c:\folder\file.txt") do (
for %%b in (%%a) do move "%%~b" "d:\existing\new\folder"
)
The code below should work to move all files in except the ones in the list.
It adds a hidden attribute to the files in the list, moves all the other files, then removes the hidden attributes again.
#echo off
for /f "usebackq delims=" %%a in ("c:\folder\file.txt") do (
for %%b in (%%a) do attrib +h "%%~b"
)
cd /d "c:\folder"
move *.* "d:\already\existing folder"
for /f "usebackq delims=" %%a in ("c:\folder\file.txt") do (
for %%b in (%%a) do attrib -h "%%~b"
)
Test code for Windows 2012 as mentioned in the comments
#echo off
(echo "c:\widget1\test 1.txt" "2:\widget2\test 2.doc")>"file.txt"
for /f "usebackq delims=" %%a in ("file.txt") do (
for %%b in (%%a) do echo move "%%~b" "d:\existing\new\folder"
)
pause
You could use the following batch file split.bat and call it redirecting the content of your text file into it and redirecting the output into another file like split.bat < files.txt > newfiles.txt:
#echo off
set /P INFILE=
call :SPLIT %INFILE%
exit /B
:SPLIT
shift
if "%~0"=="" exit /B
echo "%~0"
goto :SPLIT
If you do not provide an input file (< files.txt) the scripts prompts you for a space-separated list.
If no output file is given (> newfiles.txt), the created new-line-separated list is shown on screen.
Notice that this does not verify whether your input file fulfills the described formatting.
This method is limited to a list length of 1021 bytes (characters), everything after will be truncated!
Assuming you can guarantee that each file path is enclosed within double quotes, then you just need to tweak your REPL.BAT command a bit:
type "files.txt" | repl "(\q.*?\q) *" "$1\r\n" x >"newfile.txt"
But REPL.BAT has been superseded by JREPL.BAT - it has even more functionality, and a slightly different syntax.
A JREPL solution can be as simple as:
jrepl "\q.*?\q" $0 /x /jmatch /f file.txt /o newfile.txt
If you want, you can overwrite the original file with the result by specifying - as the output file.
jrepl "\q.*?\q" $0 /x /jmatch /f file.txt /o -
If each line in the original file is <8k, then the following pure batch script should work, and it is pretty simple:
#echo off
>newfile.txt (
for /f "delims=" %%A in (files.txt) do for %%B in (%%A) do echo %%B
)
I am working on a batch script to move files from one master directory which has 1000+ files to sub folders, according to the file name, sub folders have to be created and moved accordingly. Below is the scenario/ file name format.
title_or_work_done_by_user_name.xls
From this file name pattern, I have to pick "user_name" and create a folder for that user_name. I found similar code, but not able to break it exactly at the last 'by'.
#ECHO OFF
SETLOCAL
SET "sourcedir=E:\Source"
SET "destdir=E:\Destination"
FOR /f "tokens=2*delims='by_'" %%a IN ('dir /b /a-d "%sourcedir%\*by_*.xls" ') DO (
ECHO %%a
ECHO(MD "%destdir%\%%a" 2>nul
ECHO(MOVE "%sourcedir%\*by_%%a.xls" "%destdir%\%%a\")
pause
GOTO :EOF
Can some one please help me out in extracting 'user_name' by splitting it at the last occurrence of 'by_'.
Thanks in advance :)
The DELIMS option specifies a list of characters, not a string. So your FOR loop will split tokens at ' or _ or b or y. Also, you have no way of knowing what is the number for the last token. Your design is a dead end.
Option 1
Here is a pure batch solution that will do what you want. I use substitution to convert the file name into a pseudo path. It is then easy to pick off the desired name. Delayed expansion is used in order to access the value of a variable within the same loop (code block) that sets it. The only tricky part is toggling delayed expansion on and off as needed so as to preserve any !. A FOR variable containing the ! character will be corrupted if it is expanded while delayed expansion is enabled.
#echo off
setlocal disableDelayedExpansion
for %%F in (*_by_*.jpg) do (
%= Initialize name without extension =%
set "name=%%~nF"
%= Convert "Part1_by_Part2_by_Name" into "Part1\Part2\Name" =%
setlocal enableDelayedExpansion
for %%f in ("!name: - =\!") do (
%= Only execute endlocal on the first iteration =%
if "!!" equ "" endlocal
%= The name might contain a dot, so need name and extension =%
set "name=%%~nxf"
)
set "file=%%F"
setlocal enableDelayedExpansion
%= Hide error message if folder already exists =%
md "!name!" 2>nul
move "!file!" "!name!"
endlocal
)
Option 2
The logic is simpler if a subroutine is used, as it avoids delayed expansion issues. The CALL makes the code less efficient (slower), but that shouldn't be an issue for a task like this.
#echo off
setlocal disableDelayedExpansion
for %%F in (*_by_*.jpg) do call :moveFile "%%F"
exit /b
:moveFile
set "name=%~n1"
for %%F in ("%name:_by_=\%") do set "name=%%~nxF"
md "%name%" 2>nul
move %1 "%name%"
exit /b
Option 3
The simplest solution is to use my JREPL.BAT utility - a hybrid JScript/batch script that performs regex replacement. JREPL is pure script that runs natively on any Windows machine from XP onward.
#echo off
for /f "tokens=1,2 delims=: eol=:" %%A in (
'dir /b /a-d *_by_*.jpg ^| jrepl "^.*_by_(.*)\.jpg" "$&:$1" /i'
) do (
md "%%B" 2>nul
move "%%A" "%%B"
)
#ECHO OFF
SETLOCAL
FOR %%a IN (
title_or_work_done_by_user_name.xls
title_or_work_done_by_digby_hill.xls
title_or_work_done_by_hook_or_by_crook.xls
) DO CALL :process %%a
GOTO :eof
:process
SET "name=%~1"
:: This is the actual processing
ECHO processing "%name%"
SET "name=%name:_by_=.%"
:loop
FOR /f "tokens=1,2,3*delims=." %%p IN ("%name%") DO IF "%%s"=="" (SET "user_name=%%q") ELSE (
SET "name=%%q.%%r.%%s"&GOTO loop
)
ECHO extracted name is "%user_name%"
GOTO :EOF
I've chosen to use the string _by_ as the separator, since there are names that end "by".
Simply replace the string _by_ with a string that won't occur (or has a restricted use) in the filename. I chose . byt perhaps with sme modifications (like removing the extension from the name using %~n : could be used.
The reult is [string.]*required_name.xls
By repeatedly removing the first token using . as a separator, when there is no 4th+token, then the second token would be the required string.
I have a log file I need to process and extract data from. Each line contains a string of an event log output. Unfortunately, the parts of the string is NOT uniformly formatted. Here are a few example lines:
"Some random length string. 0x8dda46 0x1 0x384 C:\Program Files (x86)\some\path\foo0.exe "
"Some random leeeength string. 0xa95ac2 0x8cc C:\Program Files (x86)\some\path\foo1.exe %%1936 0xcc0 "
"Some random leength string. 0xbcd668 0x330 C:\Program Files (x86)\some\path\foo2.exe %%1936 0xf38 "
"Some random leeeeeeeength string. 0xbcd668 0x1 0x330 C:\Program Files (x86)\some\path\foo2.exe "
"Some random leeength string. 0x352c44 0xfc0 C:\Program Files (x86)\some\path\foo3.exe %%1936 0x92c "
"Some random leeeeength string. 0xa95ac2 0x0 0x8cc C:\Program Files (x86)\some\path\foo1.exe "
"Some random leength string. 0x352c44 0x0 0xfc0 C:\Program Files (x86)\some\path\foo3.exe "
I need to extract the "foo.exe" file name without the full path and the HEX value just before the "C:\Progra..." (it's the process ID)
so I want the output be:
0x384 foo0.exe
0x8cc foo1.exe
0x330 foo2.exe
0x330 foo2.exe
0xfc0 foo3.exe
0x8cc foo1.exe
0xfc0 foo3.exe
I'm trying to achieve the goal with as less "hard coded" search/replace as possible since many parts of the string is not going to be the same content or same length. I tried to use FOR /F to split the string, but I have no way to locate the two columns as they are always changing. Only thing is constant is the "C:\Program Files (x86)" part. (Plus FOR has a 52 variable limit)
I have written some tricky batch files, but I'm starting to think I'm asking too much of DOS ;-)
Thanks in advance for any help!
#ECHO OFF
SETLOCAL
FOR /f "tokens=1*delims=." %%a IN (q28333414.txt) DO (
FOR /f "tokens=1*delims=:" %%c IN ("%%~b") DO CALL :process %%c&CALL :report "%%d
)
GOTO :EOF
:process
SET hexval=%~3
IF DEFINED hexval shift&GOTO process
SET "hexval=%~1"
SET "drive=%~2:"
GOTO :eof
:report
SET "line=%drive%%~1"
SET "line="%line:.exe=.exe"%"
FOR %%r IN (%line%) DO ECHO %hexval% %%~nxr&GOTO :eof
I used a file named q28333414.txt containing your data for my testing.
The first process simply throws away each (space-delimited) parameter between the . and : until there are exactly two left - the required hexval and the drive letter.
The report process re-attaches the drive letter and encloses it and the .exe name in quotes. the for %%r picks the first string, shucks off the quotes, spits out the result and all's done.
Edit : fixed report to show name and extension of file only as required and dbenham comment
Breaking news: (literally!)
#ECHO OFF
SETLOCAL enabledelayedexpansion
FOR /f "delims=" %%a IN (q28333414.txt) DO SET "line=%%~a"&CALL :process "!line::=" "!"
)
GOTO :EOF
:process
SET "hexval=%~3"
IF DEFINED hexval shift&GOTO process
CALL :lastbar1 %%~1
SET "filename=%~2"
SET filename="c:%filename:.exe =.exe" %
FOR %%r IN (%filename%) DO ECHO %hexval% %%~nxr&GOTO :eof
GOTO :eof
:lastbar1
SET "hexval=%~3"
IF DEFINED hexval shift&GOTO lastbar1
SET "hexval=%~1"
GOTO :eof
OK - let's try this, then.
For each line, replace all evil colons with " " and pass resultant quoted-string sequence to a subroutine.
Shift the parameters until there are but 2, which will be the string before and after the final countdown - er, colon.
Repeat the process for the first parameter. The penultimate value is the required hexval.
with the second parameter, add "c: before and " after any .exe, so the result is a quoted full-filename and dross; spit out the hexval and filename and done...
small revision in the rather dim light of the "&" comment - the famous set "var=whatever" formula fails with & included in this case (as in subdirectory "Documents & Settings") so the enclosing quotes can be removed as trailing spaces are not relevant. Would have been useful to know what the test data triggering the problem was though - reduces guesswork.
Any good regex utility you can lay your hands on should be able to solve your problem. I like to use my JREPL.BAT hybrid JScript/batch utility. It is pure script that runs natively on any Windows machine from XP onward.
Assuming your file is test.log, then I would use:
jrepl ".* (0x[0-9A-F]+) C:\\Program Files \(x86\)\\(?:.*\\)?([^\\]+\.exe) .*" "$1 $2" /i /f test.log
On each line it looks for the last occurrence of a hex string sandwiched by spaces that precedes a file path that begins with "C:\Program Files (x86)\" and ends with ".exe". I made the search ignore case.
This solution assumes that there are not backslashes into the random string.
#echo off
setlocal EnableDelayedExpansion
for /F "tokens=1-5 delims=\" %%a in (logFile.txt) do (
rem Extract the HEX value
for %%A in (%%~a) do (
set "value=!lastButOne!"
set "lastButOne=%%A"
)
rem Extract the file name
for /F %%A in ("%%e") do set "name=%%A"
echo !value! !name!
)
Here's a hybrid batch + JScript script (but still a .bat file) that will perform a regexp replace similar to NextInLine's PowerShell solution.
#if (#CodeSection == #Batch) #then
#echo off
setlocal
set "logfile=test.log"
rem // Ask JScript to parse log. On each line, %%I = hex. %%J = exe.
for /f "tokens=1*" %%I in ('cscript /nologo /e:JScript "%~f0" "%logfile%"') do (
echo %%I %%J
)
rem // End main runtime.
goto :EOF
#end
// JScript chimera portion
var fso = WSH.CreateObject('Scripting.FileSystemObject'),
log = fso.OpenTextFile(WSH.Arguments(0), 1);
while (!log.AtEndOfStream) {
var line = log.ReadLine();
WSH.Echo(line.replace(/^.+(0x[0-9a-f]+) \w:\\.+?\\(\w+\.exe).+$/i, "$1 $2"));
}
log.Close();
Course if I were in your boat I'd probably use GnuWin32 sed.
sed -r -e "s/^.*(0x[a-f0-9]+) \w:.+\\(.+\.exe).*$/\1 \2/i" test.log
Just for giggles, I ran some time tests of each fully-working solution against the O.P.'s test log file above, running each several times and getting the mode duration (the result occurring most often).
Aacini's solution: 0.013s (Excellent, but depends on narrow matches)
sed: 0.015s (simplest)
Magoo's solution: 0.034s (clever!)
my JScript hybrid: 0.034s (the best, of course)
dbenham's jrepl.bat: 0.051s (powerful Swiss army knife solution)
NextInLine's PowerShell: hanged my timer script, but felt like about a half a second after the initial painful priming of PowerShell
This is really a task that calls for regular expressions, and for regular expressions at the windows command-line you want powershell. Fortunately, you can run powershell from a batch file or the DOS command-prompt:
powershell -Command "(Get-Content 'c:\full_path_here\input.log') -replace '.+?(0x[0-9a-f]{3}) .+?\\([^\\]+\.exe).*', '$1 $2'"
This has a few parts
powershell -Command runs the entire expression in quotation marks as though it were run from the powershell command line
Get-Content is like the linux cat command - it reads the entirety of the file contents
-replace uses regular expressions to replace the content on each line of the file with the two matched expressions in parentheses
for /f %a in ('REG QUERY HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run') do #echo %a|find /v "\"
OUTPUT:
XSENZ
Persistence
IntelliPoint
IgfxTray
HotKeysCmds
I need to Parse it one by one and save it into CStringArray .Is it possible? please give me solution
Simply use the API.
RegEnumValue will do the Job.
Your solution above truncates the names in some cases.
This uses a helper batch file called repl.bat to remove the leading and trailing spaces - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
#echo off
setlocal EnableDelayedExpansion
for /f "skip=2 delims=" %%a in ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" ^|repl " *(.*?) *REG_.*?.*" "$1" ') do (
set /a c+=1
set n=000!c!
set n=!n:~-2!
set "var[!n!]=%%a"
)
set var[
pause
From a batch file I want to extract the number 653456 from the following string:
C:\Users\testing\AppData\Local\Test\abc123\643456\VSALBT81_COM
The number will change, however it will always be just digits.
My current theory is to search for something that fits \alldigits\, then replace the two \s with white space, but I can’t quite get it.
Assuming the number is always the parent folder (the folder before the end):
#echo off
set "str=C:\Users\testing\AppData\Local\Test\abc123\643456\VSALBT81_COM"
for %%F in ("%str%\..") do set "number=%%~nxF"
EDIT - Code sample adapted to correct errors shown in comments
set d=C:\Users\testing\AppData\Local\Test\abc123\643456\VSALBT81_COM
for %%f in ("%d:\=" "%") do for /f %%n in ('echo %%f^|findstr /b /e /r "\"[0-9]*\""') do (
echo %%~n
)
Just precede the path with a quote, split the path, replacing each backslash with a quote a space and a quote and append a quote (so we have a list of elements to iterate), and for each part check if it is formed only by numbers
#echo off
setlocal EnableDelayedExpansion
set "string=C:\Users\testing\AppData\Local\Test\abc123\643456\VSALBT81_COM"
for /L %%d in (0,1,9) do set "string=!string:\%%d=\ %%d!"
for /F "tokens=2" %%a in ("%string%") do for /F "delims=\" %%b in ("%%a") do echo Number: [%%b]
This uses a helper batch file called repl.bat from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
#echo off
set "string=C:\Users\testing\AppData\Local\Test\abc123\643456\VSALBT81_COM"
echo "%string%"|repl ".*\\([0-9]*)\\.*" "$1"
Here is how I striped numbers from a string in batch (not a file path, should be generically working for a "string")
#ECHO OFF
::set mystring=Microsoft Office 64-bit Components 2013
set mystring=Microsoft 365 Apps for enterprise - en-us
echo mystring = %mystring%
for /f "tokens=1-20 delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!##$&*()-= " %%a in ("%mystring%") do (
IF %%a == 64 (
set ONum=%%b
GoTo varset
)
IF %%a == 32 (
set ONum=%%b
GoTo varset
)
set ONum=%%a
)
:varset
echo numfromalphanumstr = %numfromalphanumstr%
pause
https://www.dostips.com/forum/viewtopic.php?t=3499
https://superuser.com/questions/1065531/filter-only-numbers-0-9-in-output-in-classic-windows-cmd
Extract number from string in batch file
How to extract number from string in BATCH