How to copy files from sub folder only using cmd? - batch-file

I have a folder structure like:
-MainDir
-f0
-f0
-f0
-a.txt
-b.txt
-c.txt
-f1
-f1
-f1
-aa.txt
-bb.txt
-cc.txt
If you see above my files are in 3rd folder in terms of hierarchy. Now my requirement is to be able to copy only the main folder and it's files to another location. For example: output for above structure should be as following:
-MainDir
-f0
-a.txt
-b.txt
-c.txt
-f1
-aa.txt
-bb.txt
-cc.txt
but when I try using XCOPY, all the folders get copied as it is. So I am trying to figure out a way to achieve my target folder structure as shown above

The following will loop the directories under main, find all files, and put them all in the top most sub directory in destination as you're looking for:
CMD Script:
#(SETLOCAL
ECHO OFF
SET "_MainDir=C:\Main\Dir"
SET "_DestDir=%Temp%\Destination\Main\Dir"
)
FOR /D %%A in (
%_MainDir%\*
) DO (
IF NOT EXIST "%_DestDir%\%%~nxA" MD "%_DestDir%\%%~nxA"
FOR /F %%a IN ('
DIR /A-D /S /B "%%~fA\*"
') DO (
ECHO Copying: "%%~fa"
ECHO To: "%_DestDir%\%%~nxA\%%~nxa"
COPY /B /V /Y "%%~fa" "%_DestDir%\%%~nxA\%%~nxa"
)
)

This is a quite easy task using xcopy together with a for /D loop, given that the three sub-directories in each hierarchy branch are equally named (like f0\f0\f0 and f1\f1\f1):
rem // Loop through immediate sub-sirectories of main directory:
for /D %%I in ("D:\MainDir\*") do (
rem /* Build path to files into second sub-directory (though there are no files);
rem the third sub-directory and its contents is regarded due to the `/S` option;
rem the copy destination directory then receives the whole contents of the
rem second source sub-directory, including the third source sub-directory: */
xcopy /S /I "%%~I\%%~nxI\*.*" "D:\CopyDir"
)

Related

Batch how to move hidden directories?

I am using the following script lines in a batch script (.bat) to copy the contents of a directory (foo) to another (bar):
move "C:\foo\*.*" "C:\bar\"
for /d %%a in ("C:\foo\*") do move "%%~fa" "C:\bar\"
The first line moves files and the second lines moves folders. However, these aren't moving the hidden directories. .git is a common example. I tried for /d %%a in ("C:\foo\.*") do move "%%~fa" "C:\bar\" with no success.
How can I move my hidden directories along with the rest of my files and directories?
EDIT: The following solution is very close to doing what is required, but fails because the "move" command can't find the hidden folder (tried the same on a .folder that wasn't hidden and it worked):
for /f "tokens=*" %%G in ('dir /b /a:hd "C:\foo\*"') do move "C:\foo\%%G" "C:\bar\"
For what you are trying to do, you can first use the attrib command before moving things to remove hidden attributes from files. You can use this to accomplish your goal:
attrib -h "C:\Program Files\Git\usr\tmp\*.*"
move "C:\Program Files\Git\usr\tmp\*.*" "C:\Program Files\Git\usr\bin\"
To do this with other things, you can do this:
attrib -h "<SourceParentFolder>\*.*"
move <source> <destination>
NOTES: You should note #Mark's comment. Using C:\file\path\folder\* is not correct. You should use C:\file\path\folder\*.*. For more information view #Mark's comment
For more information on attrib use attrib /? or check this
A file or directory name beginning with . does not mean that it is hidden.
Anyway, for/for /D iterates over non-hidden files/directories. However, dir allows to return hidden items as well when using its /A option, which can be made use of by using for /F:
rem // Change into source directory:
pushd "C:\Program Files\Git\usr\tmp" && (
rem // Iterate over all directories, even hidden and system ones:
for /F "delims= eol=|" %%I in ('dir /B /A:D-S-L ".*"') do (
rem // Actually move the directory:
move "%%I" "C:\Program Files\Git\usr\bin\"
)
rem // Return from source directory:
popd
)
After some extra research, I found that robocopy seems to be included by default in Windows 10 distributions and robocopy /MOVE allows moving all the needed files and folders in a single line, such as:
robocopy "C:\foo" "C:\bar" /E /MOVE
Additional logging options can be added to reduce the output to the command line.

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".

Need Batch script to search and copy specific file from network drive

I am very new to batch scripting.
I need to write a batch script to search for a specific file(usually .zip or .7z extension) located on network drive directory(containing multiple folder and sub-folders with space in name) and copy the same to my local drive.
Also I need to copy a zip file containing "elf" keyword which will also be located in the same directory where my file is present.
For example:
Search file: abc.zip
Network drive:\abc.com\test
So basically I need to search for my file abc.zip in the network directory(including sub-folders) and if found copy the same to my local drive(say c:\Temp) and also I need to copy *elf* file to the same local directory.
Thanks in Advance.
#echo off
rem Prepare environment
setlocal enableextensions
rem Configure paths
set "source=\abc.com\test"
set "target=c:\test"
rem Change drive/path to source of files
pushd "%source%"
rem Recurse folders searching adecuated files and when found, copy to target
for /f "tokens=*" %%f in ('dir /s /b "abc.*z*" ^| findstr /i /e /c:".zip" /c:".7z"') do (
copy /y "%%~ff" "%target%"
if exist "%%~dpf\*elf*.zip" copy /y "%%~dpf\*elf*.zip" "%target%"
)
rem Return to previous drive/path
popd
rem Clean
endlocal
This will search the source folder hierarchy for indicated filename and an aproximate match based on file extension (.*z* matchs .zip, .7z and more), filters file extensions to only accept the cases needed (.zip and .7z) and copy the required files to target folder.
Have you considered using the Extended Copy Utility (xcopy)? Syntax is as simple as
xcopy "<directoryToCopyFrom>" "<directoryToCopyTo>" /C /S /D /Y /I
This will work assuming you're wanting to write a Windows batch script.
Searching for the "elf" string will be a bit trickier though. You might consider doing that using Java, and then call the batch file from the Java program.
for /d /r "drive:\abc.com\test" %%A in (*) do (
if exist "%%~A\abc.zip" copy "%%~A\abc.zip" "C:\Temp"
if exist "%%~A\*elf*" copy "%%~A\*elf*" "C:\Temp"
)

Windows batch move (or copy) PDF file by name to corresponding folder

I am trying to find a way to create a Windows batch script that will look at a target folder full of .pdf files and move (or copy) them to another directory with existing subfolders based on the filename.
The files and folders are names of actual people. I want to be able to get that person's pdf into their existing folder using a script.
Say I have 2 files in my folder; smithJohn015.pdf and thomasBill030.pdf.
I would like to be able to put smithJohn015.pdf into folder SmithJohn and thomasBill030.pdf into folder ThomasBill.
I don't want the script to create new folders or overwrite existing files if there's a duplicate filename.
I'm not necessarily looking for anyone to write a script for me, but if anyone can just get me started in the right direction it would be appreciated.
Try modifying this answer for your evil purposes.
#echo off
setlocal
pushd "c:\path\to\PDFs"
for /d %%I in (c:\Path\To\People\*) do (
for %%F in (*) do (
for /f %%A in ('echo %%~nF ^| find /i "%%~nI"') do (
set /p a="Moving %%F to %%I... "<NUL
move "%%F" "%%I" >NUL
echo Done.
)
)
)
popd
You'll need to add a check for if not exist pdffile before the move, but there's a starting direction for you anyway.
The following assumes the target subfolders' location contains only the subfolders where the PDFs may go and that every PDF that you want to move has a name formatted as the corresponding subfolder's name followed by exactly three characters (same as in your examples):
#ECHO OFF
FOR /D %%D IN ("D:\path\to\subfolders\*") DO (
MOVE "D:\path\to\PDFs\%%~nD???.pdf" "%%D"
)
Or as a one-liner to execute directly at the command prompt:
FOR /D %D IN ("D:\path\to\subfolders\*") DO (MOVE "D:\path\to\PDFs\%~nD???.pdf" "%D")
folderlist.txt contains all names of folders in which you want to copy respective PDFs.
xcopy is copy command. format xcopy "source" "destination" /parameters.
pause is just to keep command window on
for /F "tokens=*" %%A in (folderlist.txt) do (
xcopy "E:\path\to\source folder\<prefix>%%A<suffix>.pdf" "E:\path\to\destination folder\<prefix>%%A<suffix>\" /s)
pause
You can use wildcards in source & destination paths.

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