recursive move command on windows - windows

I need to do a .bat copy of a .sh, I don't know much Windows cmd.
On Linux I could do
mv ...
or
rsync -a SOURCE/ DEST/ --remove-sent-files --ignore-existing --whole-file
but Windows "move" can't do the same
maybe there is a windows simple alternative, simpler and more performant than
for /R c:\sourceFolder\ %%G in (*) do ( move /Y "%%G" c:\destinationFolder\ )
Linux mv seems to update directories pointer, but the above Windows command will do hard stuff? I guess it's not a good idea for the big folders I need to frequently move

Robocopy did wonders for me:
robocopy c:\cache c:\cache-2012 ?????-2012*.hash /S /MOV
I used it to move all files with certain mask out of c:\cache and its numerous subdirectories.

The move command can move directories as well as files.
cd /d C:\sourceFolder
rem move the files
for %%i in (*) do move "%%i" C:\destinationFolder
rem move the directories
for /d %%i in (*) do move "%%i" C:\destinationFolder

I know this is an old thread, but since it does not have a correct answer I figured I'd tie it off.
The old DOS command to accomplish this is:
move <source directory> <destination directory>
So in the OP question:
move C:\sourceFolder c:\destinationFolder
The folder and everything in the folder (including sub-directories) will be moved.

XCOPY should do the trick, I use it it in batch files all the time
something like, if you're just trying to target .sh files
XCOPY /E /H /Y /C "%SOURCEDIR%\*.sh" "%TARGETDIR%"
Let me know if you have more questions

#echo off
setlocal
set DIR=
set OUTPUTDIR=C:\Documents and Settings\<username>\Desktop\sandbox1\output
for /R %DIR% %%a in (*.jpg) do xcopy "%%a" "%OUTPUTDIR%"

For recursive move in windows, a simple move command is ok. Here is the example, I think it would be helpful.
move D:\Dbbackup\*.dmp* D:\Dbbackup\year\month\
Where .dmp is the extension of the file that would be moved to the location recursive folder Dbbackup , then year, then month.

Related

How to delete an intermediate directory in windows using cmd?

For example to go from this:
directory1/
101/online/img1.png
102/online/img2.png
103/online/img3.png
To this:
directory1/
101/img1.png
102/img2.png
103/img3.png
In this example, 101 (and 102, 103...) directories contain only a single directory online, which is always of the same name. However, online directory may contain multiple files inside, but no directories.
I am looking for an automated way to manipulate my files like that, as I have a big list of such cases. I am looking for a Windows Command Prompt or Powershell solution.
New answer and updated
This from the command line:
pushd directory1
for /D %D in (*) do move /Y %D\online\*.* %D && rmdir /s /q %D\online
popd
Or this from a cmd file:
pushd directory1
for /D %%D in (*) do move /Y %%D\online\*.* %%D && rmdir /s /q %%D\online
popd

Batch file to run all .bat files in folders and sub-folders with different names

I need a simple script to run all .bat files in folders and sub-folders with their different names. here is an example :
Main Folder> Bat Folder 1> Bat File 1.bat
Main Folder> Bat Folder 2> Bat File 2.bat
. . .
Main Folder> Bat Folder N> Bat File N.bat
There are many topics out there asking the same question but the one that really worked for me was the following :
#echo off
pushd C:\Users\%USERNAME%\Desktop\Bat Folder 1\
for /f "delims=" %%x in ('dir /b /a-d *.bat') do start "" "%%x"&timeout /t 2 >nul
popd
However the problem is the direct folder address. I can't manually enter the folder names and run them one by one. it would take a long time. I want the script to go through all folders and sub-folders ignoring their names and run them all. it would be better to have the script run inside the main folder instead of the folder address !
Not a full answer, but I think this might help give you a start:
for /r "C:\Users\%USERNAME%\Desktop" %%a in (*.bat) do start "" "%%~a"
You might need call "something.bat" or start "" cmd /c "something.bat"
I suppose, if your batch files need a working directory, you could try this:
:: Make sure you're in the correct drive
c:
for /r "C:\Users\%USERNAME%\Desktop" %%a in (*.bat) do (
cd "%%~pa"
call "%%~a"
)
pushd "\Path\To\Main Folder"
for /r %%F in ("*.bat") do start /wait "" "%%~F"
for /r will iterate recursively over all *.bat files in all subdirectories to any depth.
I found an alternative. I used the code dir /s /b *.bat > runall.bat to get the full directory list and then replaced them all with pushd C:\Users\%USERNAME%\Desktop\Bat Folder 1\Bat File 1.bat
It's definitely not a good way to go but at least it fixed my issue.
Edit: It simply doesn't work. just reads the first line and ignores the rest.

Windows script to backup specific files from directory to another on same machine

I need a help with mine school project scipt. I thought it would be easy, but apparently found myself a bit confused with it.
The task is to:
Write a script, which gets as parameters two directories. First directory must exist. From the first directory and its subfolders the backup will be done for files such as .c,.txt,.jpg,.csv... and these files will be backed up to the second directory, which is nonexistent or is empty.
I figured out just the copying part...
#echo
if %username%==administrator goto useradmin
rem # files with C
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.c" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
rem # files with TXT
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.txt" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
rem # files with JPG
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.jpg" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
rem # files with CSV
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.csv" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
You aren't usually provided homework/tasks for which you have not previously been provided sufficient information. When you are the intention is usually that you actually put in some time and effort researching.
For that reason I will only provide this. You can put in your own time and effort to look up the commands and work out how it works:
#Echo Off
Set/P "SrcDir=Enter Source Folder: "
If Not Exist "%SrcDir%\" Exit/B
Set/P "DstDir=Enter Destination Folder: "
ROBOCOPY "%SrcDir%" "%DstDir%" *.c *.txt *.jpg *.csv /S

Move files in specific subfolders to another subfolder

I have folder C:\Folder. In this folder I have thousands of subfolders. I want to target only those ending in cv. In these subfolders I have a file and another subfolder. C:\Folder\SubFoldercv\cv. I would like to move all the files in those subfolders to the second subfolder using CMD in Windows 10.
(So from C:\Folder\SubFoldercv to C:\Folder\SubFoldercv\cv).
Use for /D to find the directories ending with cv, and use robocopy to copy (or robocopy /mov to move) all files from a folder to another:
#echo off
FOR /D %%G IN ("C:\Folder\*cv") DO robocopy /mov "%%~G" "%%~G\cv" "*"
I'm currently not on a windows machine so I'm not able to test it but it should do the trick and move all files to the cv subfolder for all folders ending on cv.
If you want to use it on the command-line, use:
FOR /D %G IN ("*cv") DO robocopy /mov "%~G" "%~G\cv" "*"
instead.
EDIT: After the OP tested it, he has confirmed that it works.
How to do?
subfolderHolder = uploads
FOR /D %%i IN ("%MoveDirSource%\*\%subfolderHolder%\*") DO ROBOCOPY /MOV /E "%%i" "%MoveDirDestination%\%%~nxi"
but the wild card is not working

How can I recursively copy files of a specific pattern into a single flat folder on Windows?

I need to copy a set of DLL and PDB files from a set of folders recursively into another folder. I don't want to recreate the folder hierarchy in the target folder.
I want to use built in Windows tools, e.g. DOS commands.
mkdir targetDir
for /r %x in (*.dll, *pdb) do copy "%x" targetDir\
Use /Y at the end of the above command if you are copying multiple files and don't want to keep answering "Yes".
command XCOPY
example of copying folder recursively:
mkdir DestFolder
xcopy SrcFolder DestFolder /E
(as stated below in the comment following WIKI that command was made available since DOS 3.2)
Make sure that you have the quotes right if you have spaces in your path.
This is my postbuild event for my TFS build server (that is why there is "%%"). I needed all the test files to be copied over.
if not exist "$(TargetDir)..\SingleFolderOutput" mkdir -p "$(TargetDir)..\SingleFolderOutput"
for /r **%%x** in (*.dll, *.pdb, *.xml, *.xaml, *.exe, *.exe.config) do xcopy **"%%x"** "$(TargetDir)..\SingleFolderOutput" /Y
For gathering PBD files I end up with this:
cmd /q /c for /R "<your_source_folder>" %f in (*.pdb) do xcopy "%f" "<your_destination_folder>" /I /Y
Note: must be two percent chars, if you use for /r from cmd-file:
for /r %%x in (*.dll, *pdb) do copy "%%x" targetDir\
#echo off
if %1'==' goto usage
if %2'==' goto usage
if %3'==' goto usage
for /D %%F in (%1\*) do xcopy %%F\%2 %3 /D /Y
for /D %%F in (%1\*.) do call TreeCopy %%F %2 %3
goto end
:usage
#echo Usage: TreeCopy [Source folder] [Search pattern] [Destination folder]
#echo Example: TreeCopy C:\Project\UDI *.xsd C:\Project\UDI\SOA\Deploy\Metadata
:end
I'm not aware of any command line tools that do this directly, but you could create a batch script to loop through sub folders, and copy the files you need.
However you may end up with files with duplicate filenames if you place them all in the same folder.

Resources