Move files with all subdirectories - batch-file

I want to move all files and directories to another specific directory.
|
\---MainDirectory
| MainFiles
|
+---Subdirectory1
| MultipleFiles1
|
\---Subdirectory2
MultipleFiles2
|
\---TargetDirectory
| MainFiles
|
+---Subdirectory1
| MultipleFiles1
|
\---Subdirectory2
MultipleFiles2
Currently using following code with batch file:
move "MainDirectory" "TargetDirectory"
It is moving only MainDirectory like:
|
\---TargetDirectory
MainFiles
I do not want to move MainDirectory. I know it is very basic question, but I am not expert and have searched for more than 2 hours and haven't found a solution.
I tried
robocopy "MainDirectory" "TargetDirectory" /MOVE
It is also moving only MainFiles.
This code is moving successfully but removing MainDirectory which I do not want to remove
robocopy.exe "MainDirectory" "TargetDirectory" /S /Move

Related

How do I set the working directory of a batch script to where the shortcut is invoked?

I have a simple batch script that copies the contents of ./source to a destination specified as an argument. Once the files in ./source are copied to the destination, they are moved to ./archive.
script.cmd "\\web01\www\accounting\assets" "otherArgs"
This script is going to be used to in several areas, but instead of having discrete copies of the script in each area it is being used, I want to utilize shortcuts(.lnk) to point back to one copy of the script, so that if changes are made everyone using it is using the most up-to-date version.
The folder structure would look something like this:
company
|
+-- script.cmd
|
+-- accounting
| |
| +-- archive
| | |
| | \-- oldFile
| |
| +-- source
| | |
| | \-- newFile
| |
| \-- script.lnk -> ..\script.cmd
|
+-- hr
| |
| +-- archive
| | |
| | \-- oldFile
| |
| +-- source
| | |
| | \-- newFile
| |
| \-- script.lnk -> ..\script.cmd
|
+-- sales
| |
| +-- archive
| | |
| | \-- oldFile
| |
| +-- source
| | |
| | \-- newFile
| |
| \-- script.lnk -> ..\script.cmd
|
...
Currently, the script is not using the correct archive and source directories. It is trying to look for the directories at the top level, not in the subdirectories. Is there a variable like %~dp0 or %cd% that would have the path where the shortcut resides? Or is there a way I can pass it in as an argument without using an absolute path? Such as
script.cmd "\\web01\www\accounting\assets" "%cd%" "otherArgs"
Use an alias script:
#setlocal ENABLEEXTENSIONS
#cd %~dp0
call %path_to_script%\script.cmd %*
Put one of those in each of your directories. No shortcuts required.

In cmd how can I list folders that contain files with a specific extension?

Using just the command prompt or a batch script, I would like to look inside my current directory and all subdirectories for folders containing a specific filetype.
So, example:
Say I am in directory F:\dir\one\, and F:\dir\one\ looks like this:
F:\dir\one\
|
+--F:\dir\one\two\
| |
| +--file.c
|
+--F:\dir\one\three\
| |
| +--work.py
| |
| +--F:\dir\one\three\four\
| | |
| | +--file2.c
And I ran my command/script looking for *.c files, I would expect an output of
>F:\dir\one\two
>F:\dir\one\three\four
because those folders contain *.c files.
How could I do this?
To be run from command line. For batch files, percent signs need to be escaped, replacing % with %%
for /r %a in (.) do #if exist "%~fa\*.c" echo %~fa
This:
for /r %a in (.) do #if exist "%~fa*.c" echo %~fa
works for current directory but it doesn't if you specify in which directory you would like to have result:
for /r %a in ('F:\dir\one') do #if exist "%~fa*.c" echo %~fa
it just hangs. I tried 'F:\dir\one' and "F:\dir\one" and 'F:\dir\one\' and "F:\dir\one\"

Find multiple files with unique file names and move them

Im looking to get some helt creating a BAT file.
I do not have a lot of knowledge about this from before.
The situation:
I have a folder containing about 48 000 pdf files.
I have a csv file containing all the file names (aprox 15000 unique file names) i want to separate and move to another folder.
The files are named like this: 0723850734;0732332262;0723846680;0736187285;0733628507.... and so on.
I created this .BAT file to copy one file:
copy /-y "D:\Scrive\Data\Done\PDF\0723850734.pdf" "D:\Scrive\Data\Done\Telenoravtal"
pause
How can i add all my file names i want to copy/move?
This is more a hack or a workaround than a real solution.
You can open the .csv file in Excel and create template using first part of your code in Column A and drag it as far as you need then in Column B having the file name and in Column C having the rest of your code.
_____________Column A _____________|__Column B__|______________Column C____________________
copy /-y "D:\Scrive\Data\Done\PDF\ | 0723850734 | .pdf" "D:\Scrive\Data\Done\Telenoravtal"
copy /-y "D:\Scrive\Data\Done\PDF\ | 0000000000 | .pdf" "D:\Scrive\Data\Done\Telenoravtal"
copy /-y "D:\Scrive\Data\Done\PDF\ | 1234567890 | .pdf" "D:\Scrive\Data\Done\Telenoravtal"
copy /-y "D:\Scrive\Data\Done\PDF\ | 0987654321 | .pdf" "D:\Scrive\Data\Done\Telenoravtal"
Afterwords just copy the results to text editor, remove extra spaces and your bat script is done.

What am I doing wrong with my .bat that deletes a folder?

I have a folder named "Red" and in it is a jpeg. I want to delete this folder, and the picture inside, but I don't want it to ask me if i want to delete it, I just want the program to delete it and move on.
It's located on my desktop.
Here is the code I have to delete the folder and picture
del /Q "C:\Users\Chris\Desktop\Red"
When I run the .bat it doesn't do anything, it just opens the CMD and closes. Is there a problem with the syntax? Thanks!
The del command is not the right tool for deleting a folder and its contents. Rather you should use rmdir (or rd) instead, with the option to delete subfolders/files (and without prompting):
rmdir /s /q whatever
The del command will work quietly without prompting and it will descend through directory structures but it will only delete files in those structures. If you set up the following hierarchy:
xyzzy
|
+----- xyzzy.txt
|
+----- plugh
|
+----- plugh.txt
|
+----- twisty
|
+----- twisty.txt
and then run del /s /q xyzzy, all you will see is:
Deleted file - C:\Users\Pax\Documents\xyzzy\xyzzy.txt
Deleted file - C:\Users\Pax\Documents\xyzzy\plugh\plugh.txt
Deleted file - C:\Users\Pax\Documents\xyzzy\plugh\twisty\twisty.txt
and you'll be left with the tree untouched (but all the files gone):
xyzzy
|
+----- plugh
|
+----- twisty
If instead you use rmdir /s /q xyzzy, the whole tree (including the top level) will be removed.

Batch file not generating output as expected?

I have a simple dynamically generated batch file which follows the general format of:
#echo off
dir /a/s "C:\inetpub\wwwroot\files\clients\26\properties" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\pFiles_26.dat"
dir /a/s "C:\inetpub\wwwroot\files\clients\26\adverts" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\aFiles_26.dat"
dir /a/s "C:\inetpub\wwwroot\files\clients\28\properties" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\pFiles_28.dat"
dir /a/s "C:\inetpub\wwwroot\files\clients\28\adverts" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\aFiles_28.dat"
dir /a/s "C:\inetpub\wwwroot\files\clients\32\properties" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\pFiles_32.dat"
dir /a/s "C:\inetpub\wwwroot\files\clients\32\adverts" | findstr "File(s)" > "C:\inetpub\wwwroot\scripts\foldersize\aFiles_32.dat"
and so on... there's about 280 different folders it will need to run through... but the resulting files generated e.g. pFiles_26.dat or the like all end up as zero length files...
I know all these directories to have some content in them, so the results should all be varied...
As far as I can tell the commands aren't firing off the way they should... how can I ensure that these commands actually fire off, one by one, and result in files that contain the actual output of the recursive values of the numbers of files and total directory sizes??
Check you have write permission to the destination folder. Try your code to a desktop folder, for example. Mind you, you shouldn't get zero byte files either if that were the case.

Resources