Add files to folders - batch-file

I have fildes witht he names:
fd3_xxx
fd3_xx1
fd3_xx2
fd6_xxx
fd6_xx1
fd6_xx2
How can I write a batch file that would put all the "fd3" files in a folder called "fd3", and all the "fd6" files into a folder called "fd6'.

#echo off
for %%a in (*.*) do (
for /F "delims=_" %%b in ("%%a") do (
if not exist "%%b" md "%%b"
move "%%a" "%%b"
)
)

This should do it
xcopy fd3_* fd3 /i
xcopy fd6_* fd6 /i
del fd3_* /f /q
del fd6_* /f /q
Maybe try it on some test folders first to make sure it does what you want it to.

Related

Batch script to delete all the subfolders inside specific folder in all remote servers and output as a text file

Here I am trying to execute a batch script which deletes subfolders under folder "updates" in all remote machines. List of servers are passed as input
(serverlist.txt)
echo off
for /f %%l in (C:\deleteauto\delrem\serverlist.txt) do
if exist C:\updates goto sub
if not exist C:\updates goto nofile
:sub
del /f /q "C:\updates\*.*"
for /d %%d in ("C:\updates\*.*") do rmdir /s /q "%%d"
echo folder is deleted in %%l >>c:\finaloutput.txt
:nofile
echo No folders in %%l >>c:\final output.txt
Can someone please help in rectifying the errors. Script executed but outputs nothing.
Try this code:
#echo off
for /f "tokens=*" %%l in (C:\deleteauto\delrem\serverlist.txt) do (
if exist "\\%%l\C$\updates" (
pushd "\\%%l\C$\updates"
del /f /q "*."
for /d %%d in ("*.") do rmdir /s /q "%%d"
echo Folder is deleted on server: %%l>>finaloutput.txt
popd
) else (
echo Folder does not exist on server: %%l>>finaloutput.txt
)
)
Here's an example of how I may tackle the task:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "SvrLst=C:\deleteauto\delrem\serverlist.txt"
If Not Exist "%SvrLst%" GoTo :EOF
(For /F UseBackQ^ Delims^=^ EOL^= %%G In ("%SvrLst%"
) Do PushD "%%~G\C$\updates" 2>NUL && (
RD /S /Q . 2>NUL
Echo Instruction to empty directory applied on %%~G & PopD
) || Echo Directory path not found on %%~G) 1>"C:\finaloutput.txt"
You will note that I have not stated that the directory was emptied. Just because an instruction was applied, does not mean that it was successful at doing so. Unless you check that the \C$\updates directory is completely empty afterwards, you should not imply that it is.
Errors were rectified by #Fire Fox , Thank you! Modified slightly & it worked as expected.
Code:
#echo off
for /f "tokens=*" %%l in (C:\deleteauto\delrem\serverlist.txt) do (
if exist "\\%%l\C$\updates" (
pushd "\\%%l\C$\updates"
del /f /q "\\%%l\C$\updates\*.*"
for /d %%d in ("\\%%l\C$\updates\*.*") do rmdir /s /q "%%d"
echo Folder is deleted on server: %%l>>C:\finaloutput.txt
popd
) else (
echo Folder does not exist on server: %%l>>C:\finaloutput.txt
)
)

Combine Folders based on Folder Name Batch

I have subfolders with the following naming convention:
000026867_20200722_222406_SS24
I want to combine the contents of all folders that share the same last portion of the name. In this case all folders ending in SS24. It will always be after the 3rd underscore, but there is a chance it might be more than 4 characters after the last underscore.
I want all the files in all the folders ending in SS24 to be in a new folder named, let's say, All_SS24
The original folders can be deleted.
#Echo off
pushd "C:\path\to\your\base\folder"
for /f "Tokens=1* Delims=-" %%A in ( 'Dir /B /AD -' ) Do If Not Exist "%%A" (
Ren "%%A-%%B" "%%A"
) Else (
Move /Y "%%A-%%B*" "%%A\" RmDir "%%A-%%B"
)
PopD
Thanks for your help.
This is more or less what you seem to want:
#echo off
pushd "C:\path\to\your\base\folder"
for /f "tokens=1-4*delims=_" %%a in ('dir /b /ad "*_*"') do (
mkdir "%%d">nul 2>&1
copy "%%a_%%b_%%c_%%d\*" "%%d" /Y
rd "%%a_%%b_%%c_%%d" /S /Q
)
popd

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
)

Delete all except two folders with .bat file

Batch
#echo off
set folder="c:\FTP\"
set keep="keep1"
set keeptwo="keep2"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (
if /i "%%~ni" NEQ %keep% if /i "%%~ni" NEQ %keeptwo% (rmdir "%%i" /s/q || del "%%i" /s/q)
)
pause
Situation
folder1/file1.txt
folder2/file1.txt
keep1/file1.txt
keep2/file1.txt
file1.txt
Expected result
I need to keep "keep1" and "keep2" folders and all included files, but "folder1" and "folder2" and "file1.txt" with all subdirectories and files must be deleted.
Current result
It removes all files in all folders, removes "folder1" and "folder2", and keeps "keep1" and "keep2"
Any clue what I'm missing.
You cannot use the /S option with the DELETE command as that will delete the file in the current directory and all subdirectories.
Regardless of that, this is how I would accomplish the task so that you don't get the error from the RMDIR command. I use an IF EXIST command to determine if it is a file or directory.
#echo off
set "folder=c:\FTP\"
set "keep=keep1"
set "keeptwo=keep2"
cd /d %folder%
for /F "delims=" %%G in ('dir /b') do (
if /I NOT "%%G"=="%keep%" (
if /I NOT "%%G"=="%keeptwo%" (
REM check if it is a directory or file
IF EXIST "%%G\" (
rmdir "%%G" /s /q
) else (
del "%%G" /q
)
)
)
)
I'm assuming that this is what you wanted to do:
#Echo Off
Set "folder=C:\FTP"
Set "keep=keep1"
Set "keeptwo=keep2"
CD /D "%folder%" 2>Nul || Exit /B
Del /F/A/Q *
For /D %%A In (*) Do If /I Not "%%A"=="%keep%" If /I Not "%%A"=="%keep2%" RD /S/Q "%%A"
Pause

Batch to find a folder in subfolders, move files one level up and delete that folder

I have this structure:
..
..\FolderA\FolderX\File1.txt
..\FolderB\FolderX\File2.txt
..\FolderC\FolderD\FolderE\FolderX\File3.txt
I need a batch to find all "FolderX" in subfolders and move all the files in "FolderX" to one level up and delete that "FolderX"
..
..\FolderA\File1.txt
..\FolderB\File2.txt
..\FolderC\FolderD\FolderE\File3.txt
How write a batch? I tried this, but is incomplete, the code not find the folders:
#Echo Off
Set _Source=%~dp0
Set _FindDir=FolderX
Set _Path=%_Source%\%_FindDir%
If Exist "%_Path%" (
Move /-Y "%_Path%\*.*" "%_Source%"
For /F "Tokens=* Delims=" %%I In ('Dir /AD /B "%_Path%"') Do Move "%_Path%\%%I" "%_Source%"
RD /S /Q "%_Path%"
) Else (
Echo There is no %_FindDir% folder in %_Source%
)
Resolved:
#echo off & setlocal enabledelayedexpansion
for /d /r %~dp0 %%a in (*) do (
if /i "%%~nxa"=="FolderX" (
set "folderpath=%%a" (
move /y !folderpath!\* !folderpath:~,-8!
rmdir !folderpath!
)
)
)

Resources