Batch: how to split string on uppercase letter - regex

I have a directory structure containing home directories named after the users full name (ForenameSurname), like:
/user/JohnDoe
/user/JaneDoe
/user/MobyDick
Now i want to copy the whole structure, changing ForenameSurname to "'first two letters of first name'+'surname'", resulting:
/user/JoDoe
/user/JaDoe
/user/MoDick
I know how to get substrings (~n), but how to split a string on the first capital letter? Is it possible at all using pure batch?

#echo off
setlocal enableextensions enabledelayedexpansion
set "root=%cd%\users"
for /d %%f in ( "%root%\*" ) do (
set "name=%%~nxf"
for /f %%a in ("!name:~0,2!"
) do for /f "tokens=* delims=abcdefghijklmnopqrstuvwxyz" %%b in ("!name:~2!"
) do if not "%%~nxf"=="%%~a%%~b" if not exist "%root%\%%~a%%~b" (
echo ren "%%~ff" "%%~a%%~b"
) else (
echo "%%~nxf" can not be renamed to "%%~a%%~b"
)
)
Rename operations are only echoed to console. If the output is correct, remove the echo that prefixes the ren command.

Try this:
#echo off
setlocal EnableDelayedExpansion
set "upcaseLetters=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
cd \user
for /D %%a in (*) do (
call :convert name=%%a
echo New name: !name!
)
goto :EOF
:convert
set "var=%2"
:nextChar
set "char=%var:~2,1%"
if "!upcaseLetters:%char%=%char%!" equ "%upcaseLetters%" goto end
set "var=%var:~0,2%%var:~3%"
goto nextChar
:end
set "%1=%var%"
exit /B

I would use my JREN.BAT regular expression rename utility - a hybrid JScript/batch script that runs natively on any Windows machine form XP onward.
jren "^([A-Z][a-z])[a-z]*(?=[A-Z])" $1 /d /t /p c:\users
The /T option is test mode, meaning it only displays the proposed rename results. Remove the /T option to actually rename the folders.

Related

batch file regex to get specific number from filename

I have a file name
pp-sssss-iiii-12.0.111.22_31-i-P.0.16.1.1
I want from the name only this (output desired):
12.0.111.22_31
then replace . with 0 and remove '_' so I got the below
1200011102231
well I tried to start from something like this
cd %cd%
for %%F in (*.txt) do echo %%~nxF >>1.txt
but I didnt know how to continue
edit , the code :
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\moudiz\Desktop\new folder\tttt"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.pbd" '
) DO (
FOR /f "tokens=4delims=-" %%d IN ("%%a") DO (
SET "modname=%%d"
SET "modname=!modname:.=0!"
SET "modname=!modname:_=!"
ECHO %%a becomes !modname!
)
)
GOTO :EOF
pause
the name of the file
P-Script-LogFiles-1.0.33.33_123-IB-P.0.16.357.1.pbd
the output 100033033123
[note: OP belatedly asked for the first token of the name to be prepended to the name generated from the original post, hence use of %%c below]
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.txt" '
) DO (
echo File "%%a"
FOR /f "tokens=1,4delims=-" %%c IN ("%%a") DO (
SET "modname=%%d"
SET "modname=!modname:.=0!"
SET "modname=!modname:_=!"
ECHO %%a becomes %%c!modname!
)
pause
)
pause
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
Using delayedexpansion, !var! refers to the modified value of the variable and set "var=!var:string=gnirts!" substitutes "gnirts" for "string" invarand assigns the result back tovar`.
Now - quite what you want to do with this modified result, you don't reveal - but guessing at renaming,
echo ren "%%a" "!modname!"
should be usable
To prepend the first token, simply change the tokens= to 1,4 *to select the first and fourth tokens) - and change the metavariable in the for to %%c (in order that the %%d processing remains the same), then use %%c which will contain the portion of the original filename before the first -.

Split a file name at last occurrence 'by'

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.

Trim extra pipes off end of text file in Windows

So basically I have a process that outputs a text file that is pipe delimited, looks something like this:
|abc123|1*|004|**gobbligook|001|%|2014-01-01|||||||||||||
This is just an example, and I'm not sure if the answer involves regular expressions. If it does i will put the actual line. Anyways
ISSUE
So for this example the import process that accepts this file is looking for 8 pipes, but there are 20, if it sees any more pipes after the 8 it's looking for the import process fails.
Question
Is there a process that I can use in a Windows environment to trim the trailing pipes off the end of this for the entire file?
UPDATE
Magoo supplied me with a great answer that I am working but I keep getting this error: Delimiter was unexpected at this time
Here is code:
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Desktop\Pipe Delimiter Project"
SET "destdir=C:\Users\Desktop\Pipe Delimiter Project"
(
FOR /f "tokens=1-7delims=|" %%a IN ('TYPE "%sourcedir%\test.txt"') DO (
ECHO(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^|
)
)>%destdir%\newfile.txt
Anyone know what's wrong? I also just put in the line from the question |abc123|..| pasted in the file like 6 times...thanks!
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=."
SET "destdir=U:\destdir"
(
FOR /f "delims=" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
SET "line=%%a"
ECHO(!line:~0,-12!
)
)>%destdir%\newfile.txt
GOTO :EOF
I used a file named q22863616.txt containing your data for my testing.
Produces newfile.txt
Assuming that the final 12 fields are all empty, for lack of information otherwise.
Another form, given additional information
#ECHO OFF
SETLOCAL
SET "sourcedir=."
SET "destdir=U:\destdir"
(
FOR /f "tokens=1-7delims=|" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
ECHO(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^|
)
)>%destdir%\newfile.txt
OK - third time's a charm.
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=."
SET "destdir=U:\destdir"
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
(
FOR /f "delims=" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
SET "$0=%%a"
SET "$1=%%a"
FOR /l %%c IN (1,1,8) DO SET "$1=!$1:*|=!"
SET "$2=%%a"
SET "$3="
SET /a tot=0
FOR /f "delims=:" %%e IN ('set $^|findstr /o /r "$"') DO SET /a tot=%%e - !tot! - 5
CALL :show !tot!
CALL ECHO %%$2:~0,-!tot!%%
)
)>%destdir%\newfile.txt
GOTO :EOF
:show
CALL SET "$3=%%$2:~0,-%1%%"
FOR /f "tokens=1*delims==" %%y IN ('set $3') DO ECHO(%%z
GOTO :eof
This seems immune to % in the data, but chokes on ! or &. You pays your money, you takes your choice...
This should eat trailing pipes but leave 8 of them
#echo off
type "file.txt"| repl "(.*?\|{8})\|*$" "$1" >"newfile.txt"
This uses a helper batch file called repl.bat - 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.

bat: Listing empty directories, if/else statements within for statements?

I'm trying to write a script that lists every file name in a specified folder, and notifies the user if that folder is empty. So far I've got:
for /r "O:\Mail\5-Friday" %%d in (*.pdf) do (
dir /a /b "%%~fd" 2>nul | findstr "^" >nul && echo %%~nd || echo Empty: Friday
)
but I've no idea where to put the if, else operators.
And is there a way to specify a folder based on user input without rewriting every function for each folder? So instead of:
if /i {%ANS%}=={thursday} (goto :thursday)
if /i {%ANS%}=={friday} (goto :friday)
:thursday
<do stuff>
:friday
<do the same stuff as thursday, but a different directory>
etc, I could write one function with variables in place of paths, assign the directory to a variable, and easily add/remove folders in the code as necessary?
To address the first part of your question, "where to put the if, else operators"... The notation of
command | findstr >nul && echo success || echo fail
... is shorthand for
command | findstr >nul
if ERRORLEVEL 1 (
echo fail
) else (
echo success
)
The magic that happens is in the conditional execution operators, the && and ||. If findstr exits with status zero, then a match was found. Therefore, execute the stuff after &&. Otherwise, status is non-zero, no match was found, so execute the stuff after ||. See how that works?
For the second part, here's a typical way to prompt the user to provide entry based on a finite number of choices.
#echo off
setlocal
:begin
set /p "day=What day? "
for %%I in (monday tuesday wednesday thursday friday) do (
if /i "%day%" equ "%%I" goto %%I
)
goto begin
:monday
call :getdirs "O:\Mail\1-Monday"
goto :EOF
:tuesday
call :getdirs "O:\Mail\2-Tuesday"
goto :EOF
:wednesday
call :getdirs "O:\Mail\3-Wednesday"
goto :EOF
:thursday
call :getdirs "O:\Mail\4-Thursday"
goto :EOF
:friday
call :getdirs "O:\Mail\5-Friday"
goto :EOF
:getdirs <path>
setlocal enabledelayedexpansion
for /f "delims=" %%I in ('dir /b /s /ad "%~1"') do (
dir /b "%%I" 2>NUL | findstr "^" >NUL || echo %%I has no files
)
goto :EOF
Or, even hacksier, I'll do something you probably weren't expecting was possible. I'll have the script open a folder selection dialog to allow the user to select the directory to scan. It's a batch / JScript hybrid script.
If you wish, you can set the root of the folder browser to a ShellSpecialConstants folder by changing the last argument in the next-to-the-last line. Using a value of 0x11 makes the root your system's drives. No value or a value of 0x00 makes the root "Desktop". Or leave the script as-is to set the root as "O:\Mail".
#if (#a==#b) #end /*
:: fchooser2.bat
:: batch portion
#echo off
setlocal
set initialDir="O:\Mail"
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%initialDir%"') do (
call :getdirs "%%I"
)
exit /b
:getdirs <path>
setlocal enabledelayedexpansion
for /f "delims=" %%I in ('dir /b /s /ad "%~1"') do (
dir /b "%%I" 2>NUL | findstr "^" >NUL || (
rem This is where you put code to handle empty directories.
echo %%I has no files
)
)
goto :EOF
:: JScript portion */
var shl = new ActiveXObject("Shell.Application");
var hint = 'Double-click a folder to expand, and\nchoose a folder to scan for empty directories';
var folder = shl.BrowseForFolder(0, hint, 0, WSH.Arguments(0));
WSH.Echo(folder ? folder.self.path : '');
Edit Since apparently BrowseForFolder accepts an absolute directory, there's really no benefit to using PowerShell / C#. The hybrid batch / PowerShell / C# script shall henceforth be retired to the revision history.
This is fun!

batch file to delete folder with special name

I want to delete all the folders with only digit name.
So I write a batch file using regular expression:
#echo off
D:
cd D:\Install\Work
for /d %%i in (*|findstr "^[0-9]*$") do (
rd /s /q %%i
)
echo [all the folders under work are deleted!]
pause
but it doesnt work. Where is the error?
#ECHO OFF
SETLOCAL
FOR /f %%x IN (
'dir /ad /b * ^|FINDSTR "^[0-9]*$" '
) DO ECHO %%x
FOR /F reads lines from a file/command output to the metavariable.
for /d simply applies dirnames to the metavariable.
Try this:
#echo off
for /r D:\Install\Work %%d in (.) do (
echo %%~nxd|findstr "^[0-9]*$" >nul && rd /s /q "%%~fd"
)
Note that this will delete all-digit folders even if they contain other folders with names not consisting of only digits!
%%~nxd: remove enclosing double quotes from %%d (~) and expand name (n) and extension (x) only
>nul: suppress output on STDOUT
%%~fd: remove enclosing double quotes from %%d (~) and expand full path (f)