batch command copy file whose name is part of a directory - batch-file

The files names are a part of the name of the directories, which can be at the end or at the beginning of the directory. For example:
pic.jpg into directory \newyork-pic
flower.gif into directory \italy-flower
computer.jpg into directory \computer-informatic
etc
Do you know a way to do that with a batch file?

Here's a piece of example code which should get you most of the way there:
#Echo Off
For /F "Tokens=1-2 Delims=-" %%A In (
'Dir/B/AD-S "*-*"^|FindStr/BRIE "[^-]*[-][^-]*"') Do For %%C In (
"%%A" "%%B") Do If Exist "%%~C.*" Move /-Y "%%~C.*" "%%A-%%B"

Related

Recursively append folder name to the files in Windows batch file

I would like to append my folder name to all the available .txt files inside a subfolder. Below is the file/directory structure. I need to achieve this in Windows BATCH script.
C:\Source\Source1\1\a.txt C:\Source\Source1\1\b.txt
C:\Source\Source1\2\a.txt C:\Source\Source1\2\b.txt
C:\Source\Source2\3\a.txt C:\Source\Source2\3\b.txt
The above files should be renamed like below:
C:\Source\Source1\1\1_a.txt C:\Source\Source1\1\1_b.txt
C:\Source\Source1\2\2_a.txt C:\Source\Source1\2\2_b.txt
C:\Source\Source2\3\3_a.txt C:\Source\Source2\3\3_b.txt
Similary, I have Source1...Source30 and under each source directory, I will have multiple folders with different numbers. I need to rename all the files under these directories and append the number(directory name) to the file name.
So far below is what I wrote:
for %%* in (.) do set CurrDirName=%%~nx*
echo %CurrDirName%
for /r %%x in (*.txt) do ren "%%x" "%CurrDirName%_%%x"
With this, I am able to achieve it in a single directory. I couldn't make it recursive. Could you guys please help me with this.
#echo OFF
SETLOCAL EnableExtensions
for /F "delims=" %%G in ('dir /B /S "C:\Source\*.txt"') do (
for %%g in ("%%~dpG.") do ECHO rename "%%~fG" "%%~nxg_%%~nxG"
)
pause
where the FOR loops are:
outer %%G loop creates a static list of .txt files (recursively), and
inner %%g loop gets the parent folder of every particular file.
The rename command is merely displayed using ECHO for debugging purposes. To make it operational, remove word ECHO (no sooner than debugged).
Moreover, I'd consider checking whether a particular file is already renamed…

Batch file moving to subfolders

I have lots of folders including images in it. Example:
C:\U2090_08
C:\U2111_08
C:\U2024_03
C:\U2024_08
C:\U2049_15
C:\U2049_35
There are 3-4 jpg files in every folder. I want to create a sub-folder called "kck" in every folder and move jpg files to this subfolder.
Example:
Before process:
C:\U2049_35\1.jpg
C:\U2049_35\2.jpg
C:\U2049_35\3.jpg
After process:
C:\U2049_35\kck\1.jpg
C:\U2049_35\kck\2.jpg
C:\U2049_35\kck\3.jpg
Here is what I am trying:
#echo off
cd %USERPROFILE%\Desktop
:: Sorting images in '\Desktop\images'
for /f "delims=" %%I in (' dir /b "%USERPROFILE%\Desktop\images\*.jpg" ') do (
if not exist "%USERPROFILE%\Desktop\images\%%~nI\kck" ( md "%USERPROFILE%\Desktop\images\%%~nI\kck" )
move "%USERPROFILE%\Desktop\images\%%~I" "%USERPROFILE%\Desktop\images\%%~nI\kck\"
)
exit
Folders including jpg files in a folder called images on desktop.
Any ideas ?
Advanced Renamer software is able to do this. I have just tried it and it have very good move options. It solved my problem.
Next code snippet could help:
#echo off
pushd "%USERPROFILE%\Desktop\images"
:: Sorting images in '\Desktop\images' (subfolders only)
for /f "delims=" %%I in (' dir /b /A:D ') do (
if exist "%%~nxI\*.jpg" (
md "%%~nxI\kck" 2>NUL
move "%%~nxI\*.jpg" "%%~nxI\kck\"
)
)
popd
exit
all paths used in dir, md and move commands are relative to the current directory which is changed to "%USERPROFILE%\Desktop\images" via the pushd command;
note 2>NUL: redirected STDERR stream in the md "%%~nxI\kck" 2>NUL command instead of testing created folder existence explicitly by if not exist "%%~nxI\kck\NUL" md "%%~nxI\kck".

batch - create directories from file names and copy these files in those directories

I have a lot of (50 000+) files in a folder. They are named as: "abc_012345678_abcabc.ext"
I need to create a directory for each of this file (all in one different directory), named as the "number part" (012345678) and then copy this file to this directory.
So in the end, i have 50000+ directories, called like "265415873" , "654879623" and so on and inside each directory is the filename with matching name (so in dir 265478951 is file called abc_265478951_abcabc.ext).
Big thanks for any advice!
EDIT:
I already created a simple script, that does what I need, so if anyone is interested, here it is:
#ECHO OFF
setlocal enabledelayedexpansion
FOR %%f in (*.ext) DO (
set filename=%%f
set folder=!filename:~0,-4!
set copypath=e:\work\programy\adresare_handa\adresare\!folder!
mkdir !folder!
copy !filename! !copypath!
)
pause
In this case, I only delete the extension and use the whole name of the file for the name of the directory, but I know, how to alter it, to read just the part of the file I need.
Thanx a lot again, if anyone have any comment for my version (pros/cons), please, do enlighten me :)
Bye!
#echo off
pushd "C:\some_dir"
for /f "tokens=1,2,3 delims=_" %%A in ('dir /b /s /a-d') do (
md "%%~B"
copy %%A_%%B_%%C %%B
)
only in case all files are with name pattern like "abc_012345678_abcabc.ext"

Batch create folders then relocate exisiting folders and subfolders using text file

I have a bunch of folders (and subfolders) in the structure of...
Test/Student001/ABC,
Test/Student001/DEF,
Test/Student002/ABC,
Test/Student002/DEF, etc...
What I then need to do is relocate those folders (and their subfolders and the files within) to another location so that it is like...
Test/Class01/ArronAmos(Student001)/ABC
Test/Class01/ArronAmos(Student001)/DEF
Test/Class02/BrettBesty(Student002)/ABC
Test/Class02/BrettBesty(Student002)/DEF
I have text files with all the folders (and subfolders) original and new names (saved like so)..
studentdata.txt
A (studentcode), B (studentnewname), C (Class)
Student001, ArronAmos (Student001), Class01
Student002, BrettBesty (Student002), Class02
Is there a way to get a batch to basically go like this (using A,B and C from text file above - preferably the one txt file if possible)...
md 'C' ::which will skip past if folder exists
rename folder 'A' to 'B' ::only rename of root folder and leave subfolders intact
move folder 'B' to 'C' ::move new named folder (B) and all subfolders and contents to new root folder (C)
The creation of the new directories and subfolders (for new and future students) goes like this and works great (except if there was a way to call a 2nd text file for the subfolder creation rather than coded in that would be awesome - but no biggy I guess)...
The Creation Batch
cd /d %~dp0 pause
FOR /F "delims=~" %%f in (dirlist.txt) DO md "%%f"
:: This code creates directories referenced from a .txt file: - :: FOR /F "delims=~" %%f in (dirlist.txt) DO MD "%%f"
pause
FOR /D %%x in (*) do mkdir "%%x\Individual Behaviour Plans" "%%x\Individual Learning Plans" "%%x\Student Reports" "%%x\Student Support Group Meetings"
:: This code creates a new dir within every folder in batch location: - :: FOR /D %%x in (*) do mkdir "%%x\value"
pause
and this is the Rename Batch that I received off one of the other techs and don't quite understand it or know to modify it to make it work..
*rename_users.bat** :: Script to Rename folders - prefixing from a text file
setlocal ENABLEDELAYEDEXPANSION
Set Rootfolder=Test Set Names=names.txt
:: Goto Root Folder cd /d %~dp0
:: Start Line Counter Set LineCount=0
:: For Every folder in the directory
For /d /r %%g in (*) DO (
:: Increment the line counter by 1 (see the use of "!" >instead of "%" due to delayed expansion)
Set /a LineCount=!Linecount!+1
:: Call the Rename Folder sub - passing through the >variables of folder name and line counter
Call:RenameFolder %%g !LineCount!)
:RenameFolder :: For all of the tokens in the findstr on the names file for /f "Tokens=1* delims=:" %%a in ('findstr /n "^" "%Names%"') DO ( :: If the line counter matches the line number If %%a==%~2 ( :: Rename the Folder Move "%~1" "%~1 %%b") ) ::Return to the Primary
Goto:EOF
Set Rootfolder= Set Names= Set linecount= Set Drive=
Endlocal
The trick is we can't just use the create directories (and subdirectories) batch file, as there are some folders existing in the original format that have data in them that we need sitting in the new structures subfolders... and moving them manually would be an option... if there were not 900+ student folders to do this too...
I hope makes some form of sense... Thanks guys!
The Batch file below do what you want:
#echo off
setlocal EnableDelayedExpansion
rem Enter into working directory
cd C:/Test
rem Process the file with original and new names
for /F "skip=1 tokens=1-3 delims=," %%a in (studentData.txt) do (
rem Get studentNewName end eliminate spaces
set "studentNewName=%%b"
set "studentNewName=!studentNewName: =!"
rem If the new folder not exist, create it
if not exist "%%c/!studentNewName!" md "%%c/!studentNewName!"
rem Move files from old folder
move "%%a/*.*" "%%c/!studentNewName!"
rem And delete old empty folder
rd "%%a"
)
Antonio

XCOPY is copying the whole directory contents, not just the files and folders mentioned in file

I am trying to copy a set of files and folders recursively from a directory. The set of files are listed in a text file. Here is my script:
set src_folder=C:\Users\mmhuqx\HW
set dst_folder=C:\Unix2Windows
set filelist=C:\Unix2Windows\filelist-tm.txt
echo Origen: %src_folder%
echo Destino: %dst_folder%
echo.
for /f "delims=" %%i in (%filelist%) do (
xcopy /s /y "%src_folder%\%%i" "%dst_folder%"
)
But When I run the script it copies the entire Directory contents including the files and folders not listed in the text file.
How can I make it work using XCOPY, or is it not possible?
Your filelist-tm.txt seems to be wrong. It contains for example an entry TOP_LEVEL\MODEM_DEBUG. So that directory is copied completely with all its files.
It seems that you only want TOP_LEVEL\MODEM_DEBUG\AENEAS_FW.fls to be copied. So your filelist-tm.txt should only contain this entry.

Resources