How to remove space from folder names in a particular directory? - batch-file

I have a requirement to remove the space from the folder name in a particular directory. e.g. Project-A is my directory under which have Phase1 Testing, Phase1 Prod, Phase1 UAT subdirectories. I want a batch script which will rename the subdirectories to Phase1Testing, Phase1Prod, Phase1UA names.

you could try something like this:
#echo off
FOR /f "delims=" %%G IN ('dir /ad /b') DO (
setlocal enabledelayedexpansion
pushd "%%~dpG"
SET fname=%%~nxG
SET fname=!fname: =!
rename "%%~nxG" "!fname!"
popd
endlocal
)
You can find more information in: replace_spaces_with_dashes and in spaces_in_file_names
I hope this help you!

Thanks #Dayana for the help. Below batch script is working fine and removing the space from the current directory.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /ad') DO (
SET Var=%%~na
SET Var=!Var: =!
REN "%%a" "!Var!"
)

Related

Batch create a Folder in every subfolders and Move all the .jpg files in it

I made this batch file that makes a folder named Pictures and puts all the .jpg files in it.
#echo off
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
end
Is there anyway to make the script iterate through subdirectories as well? I want to run the batch file on a root directory and make it work for all subfolders.
Based upon the answer you've supplied, I would suggest you could do that like this, in a single line batch-file:
#For /D %%G In (*) Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
As an alternative, because For /D may not pick up all of your directories, (it ignores directories with the hidden and system attributes):
#For /F "EOL=? Delims=" %%G In ('Dir /B /AD') Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
Actually I just figured it out
#echo off
setlocal
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
rem enter the directory
pushd %%a
echo In Directory: %%a
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
rem leave the directory
popd
)
endlocal
It does the same thing but it works through subfolders. Took me sometime to figure it out. Anyway, hopefully it'll help others too.
Try this:
for /f %%a in ('dir /b /ad "Filepath"') do (
md "%%~fa\Pictures"
for %%b in ("*.jpg") do robocopy "%%~fb" "%%~fa\Pictures\" /mov
)

moving files to folders based on extension

Finally I got to move files with different extensions into folders according to their extensions, but I cannot get rid of the "." preceding the created folders... Here's my batch file:
for %%i in (*) do mkdir "%%~xi" & move "%%i" "%%~xi"
Please help me remove the dot. Thanks
One way:
setlocal enabledelayedexpansion
for %%i in (*) do (
set ext=%%~xi
set ext=!ext:~1!
mkdir "!ext!"
move "%%i" "!ext!"
)
What you are doing is just creating a folder names after the file extension. To remove the dot all you need to do is remove the first character.
setlocal enabledelayedexpansion
for %%i in (*) do (
set "x=%%~xi"
set x=!x:~1,400!
mkdir "!x!"
move "%%i" "!x!"
)
pause `
If you by chance just want to rename the folders:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%D in ('dir /a:d /b') do (
set x=%%D
set x=!x:~1,100!
set y=!x:~0,1!
if "!y!"=="." (
rename "%%D" "!x!"
)
)
pause

Batch file - copy two last modified files

I would like to write a .bat file to move the two last modified files of a specific extension *.bak in directory a to a different directory.
I used this line to copy files:
robocopy D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups *.bak /S
I'm new with this and have no idea how to tweak this to get the result I need.
Thanks
not tested:
#echo off
for /f "tokens=* delims=" %%# in (' dir /a:-d /o:-d /t:a /b "D:\DailyBackup\IDMRObjects\SQLBackups SQLBackups\*.bak"') do (
if not defined last set "pre_last=%%~f#"
set "last=%%~f#"
)
copy /y "%last%" "c:\new_dir"
copy /y "%pre_last%" "c:\new_dir"
#echo off
setlocal EnableDelayedExpansion
cd "D:\DailyBackup\IDMRObjects\SQLBackups"
set copied=0
for /F "delims=" %%a in ('dir /B /A-D /O-D /T:W *.bak') do (
copy "%%a" "other\dir"
set /A copied+=1
if !copied! equ 2 goto break
)
:break

Create BAT file to get directory listing up to 2nd level only

I have a window directory with 300 folders inside of it and 4 levels of folders within each folder. I need a listing of folders only up to 2nd level in a txt file? I need the full path of each directory as well. I would like to use this a BAT file or CMD line prompt. Ex:
Jon Done
Test001
Tester002
Test003
Tester004
Can anyone help me to do this?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR /f "delims=" %%a IN ('dir /b /a-d 2^>nul') DO ECHO %%~fa
FOR /f "delims=" %%a IN ('dir /b /ad') DO (
PUSHD "%%a"
FOR /f "delims=" %%x IN ('dir /b /a-d 2^>nul') DO ECHO %%~fx
popd
)
popd
GOTO :EOF
This should work - you would need to change the setting of sourcedir to suit your circumstances.

Batch file to delete specific text file from multiple locations

I have a .bat file written that creates a text file within each sub-folder of the root folder that displays the sub-folder's contents. The issue I'm having now is creating a second .bat that will remove said text files from the sub-folder locations. The .bat I have for creating the .txt files are located below. Any help is greatly appreciated!
#echo off
IF EXIST "R:\Projects\000" PUSHD "R:\Projects\000\"
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') do (
dir /s/b > R:\Projects\000\%%G\Folder_Contents.txt
)
POPD
It's nearly the same as your current script.
delAllFolderContent.bat
#ECHO OFF
IF EXIST "R:\Projects\000" (
PUSHD "R:\Projects\000"
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') DO (
DEL "R:\Projects\000\%%G\Folder_Contents.txt"
)
POPD
)
I moved the FOR/F code into the IF EXIST block, so it will be executed only when the directory exists and not always.
If i truly understand you. It's look like this:
#ECHO OFF
IF EXIST "C:\Temp\" PUSHD "C:\Temp\"
ECHO #ECHO OFF > loc.bat
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') DO (
DIR /s/b > C:\Temp\%%G\Folder_Contents.txt
ECHO DEL C:\Temp\%%G\Folder_Contents.txt >> loc.bat
)
POPD
del "R:\Projects\000\folder_contents.txt" /s

Resources