How to Move a Dynamically Created folder into another folder? - cmd

I am trying to moving generated folders in which the date is dynamic for example:
results_20190821T43302.
Which would reside in a path as such:
C:\Test\results_20190821T43302
The folder "items" in which I want to move them too is: C:\Users\Admin\Desktop\items\
I have tried:
move C:\Test\results_* C:\Users\Admin\Desktop\items\
However it is returning: The filename, directory name, or volume label syntax is incorrect.
How can I resolve this? Thanks.

The move command does not accept wildcards against directories. But you can use a for /D loop to resolve wildcards, like this:
for /D %I in ("C:\Test\results_*") do #move "%~I" "%UserProfile%\Desktop\items\"
Since you have got a trailing backslash \ at the source directory, the source directory is moved into the destination directory, which must exist. If you want to move the contents of the source directory, remove the trailing \ from the destination directory and ensure this does not exist.

Thanks for the response, however I found
for /D %%G in ("C:\Test\results_*") do move "%%~G" "%UserProfile%\Desktop\items\" work for me.

Related

How to change to directory to your homedrive?(C:\, D:\, etc..)

I am writing this batch file which searches for specific a specific file. Now every time the code cd /D "%HOMEDRIVE%" is executed, it of course starts to search in that directory. But what I get is file not found.
I tried doing cd /D "%HOMEDRIVE% in the command line but it only replies where the cmd is run (e.g C:\Users\onlYUs)
How do I fix this? There is an environment variable named HOMEDRIVE whose value is C:. But it does not change to that directory. And by the way the reason why I needed that because if an instance that your homedrive is set to D: or E: it can still search for the file. Any help would be greatly appreciated!
You can't change to a directory without providing a path to a directory, and %HOMEDRIVE% only contains a drive letter. Without the backslash, it's the equivalent of typing C: at the command prompt, which only changes the drive.
You need to add the trailing path separator (backslash) to make it a directory path instead, because you're wanting to change to the root directory of that drive.
This does not work:
cd /D %HOMEDRIVE%
This does work (note the trailing backslash):
cd /D %HOMEDRIVE%\
An alternative way is pushd %HOMEDRIVE%\ which allows the batch to later popd back to the drive and directory that were initially current.

Copying a file to multiple folders in the same directory

I have a file, lets call it EXAMPLE.DBF
The location of this file is C:\EXAMPLE.DBF
Now I would like to copy this file into multiple folders at another location.
These folders are dated Sub-directories so they are named 20140101 - 20141231 and their location would be in d:\bootd\hello\20140101 - 20141231
Not only that but a file named EXAMPLE.DBF already exists in the folders...so it will ask to Copy and Replace.
I will need c:\EXAMPLE to copy and replace the existing EXAMPLE.DBF in 365 folders (20140101-20141231) in a different location.
If anyone could help me out with this I will be truly grateful.
Directly from the prompt,
for /d %a in (d:\bootd\hello\2014*) do copy /y C:\EXAMPLE.DBF %a\
Will copy C:\EXAMPLE.DBF to each directory of d:\bootd\hello\ which matches the pattern 2014* (ie. simply starts 2014), replacing any existing example.dbf in that subdirectory, if that is what you want to do.
To suppress the 1 file(s) copied messae generate for each, simply append >nul to the above line.
Just a small addition to #Magoo's answer;
in case the destination folders have spaces in their names, use double quotes like this:
for /d %a in (d:\bootd\hello\2014*) do copy /y C:\EXAMPLE.DBF "%a\"
and as pointed out by #ian0411, in case source folder has spaces in its name, use double quotes like this:
for /d %a in (d:\bootd\hello\2014*) do copy /y "C:\EXAMPLE.DBF" %a\

Using a networked drive I am trying to access a folder using a variable. I only get the contents of the previous folder.

I have a location on a networked drive that gets a folder with the current date for every file. I am trying to access this location by temporarily mapping the drive (Net Use) and then using the date to fill in the folder name.
When I execute the code it is set to the previous directory. (A DIR listing will bring up the contents of that folder and the rest of the code to move files puts them in that location)
Any ideas?
Thanks
PAUSE
REM This is to move the Export file to the FTP Site.
Set Year_Mo_Da=%date:~10,4%-%date:~4,2%-%date:~7,2%
time /t
net use z: \\Network.com\Validation\2014
REM \%Year_Mo_Da% -- This is the folder name
cd /d z:\%%Year_Mo_Da%%
DIR
Copy Z:\*FileName_*.* Y:\NewLocation\TEST
net use z: /delete
time /t
PAUSE
You're changing directories but then copying from the root after all. #foxidrive removed the backslash but perhaps it wasn't noticed when you made your change?
cd /d z:\%%Year_Mo_Da%%
...you're in the subdirectory
Copy Z:\*FileName_*.* Y:\NewLocation\TEST
...but you copied from the root of Z: after all.
This would work (quotes can't hurt of course but for this example aren't required unless your filename contains spaces):
Copy Z:*FileName_*.* Y:\NewLocation\TEST
BTW I tend to use pushd and popd so I don't have to know the drive letter. Don't know if that helps you or not but you don't have to net use /d either, or care if Z: is in use for something else.
pushd \\Network.com\Validation\2014\%Year_Mo_Da%
Copy *FileName_*.* Y:\NewLocation\TEST
popd
You don't need doubled percents here:
cd /d "z:\%Year_Mo_Da%"
Depending on what you need to do - this may be the next command:
Copy "*FileName_*.*" "Y:\NewLocation\TEST"

Windows Command Copy all files recursively to a Main Folder

I was wondering if there is a quick and dirty command that can copy all files in a directory and it's subdirectory to another folder. The destination folder will have no subdirectories just all the source files.
Also as an added bit of fun, in case there is a file name conflict, not to overwrite but to rename the destination file with something unique, maybe append _1 to the filename?
This will copy the files and prompt if there is a filename conflict.
The third party tool XXcopy has the ability to flatten a directory tree and handle filename conflicts.
#echo off
for /r "d:\folder" %%a in (*) do copy "%%a" "E:\target folder"
To copy for instance all files from the current folder and its sub directories to the parent folder of the current folder you can use:
for /r . %a in (*) do copy %a ..

Replacing a file into multiple folders/subdirectories

Is there a way in command prompt to take one file and copy it into another folder and its subdirectories based on its name?
I have an image named 5.jpg that has been put in a sub-folder that is in every folder in a directory.
I want to do a search inside of the folder (with the old image) and its sub-folders and replace all of the results with the new image.
Probably there is one more (simpler) way.
Use the replace command:
replace C:\SourceFile.Txt C:\Some_Root_Folder_Which_Contains_Multiple_SubFolders /s
As the command itself says it just replaces the file, which already existed in sub-directories.
I'm not sure if I understood you completely. The following code will search for all occurences of 5.jpg in subfolders of C:\MyPath\ and replaces them with C:\NewImage\5.jpg. I did test it, so it should work.
FOR with parameter /R will help you here:
FOR /R C:\MyPath\ %%I IN (5.jpg) DO COPY /Y C:\NewImage\5.jpg %%~fI
If you want more information about what FOR /R does and what %%~fI means, have a look at FOR /? | more which gives nice explanations about the new Windows cmd possibilities that are used here.
To do this work with several files, both paths are needed enclosed in quotes:
replace "C:\*.Txt" "C:\Some_Root_Folder_Which_Contains_Multiple_SubFolders" /s
The asterisk to make changes on all files with the ".txt" prefix.

Resources