Windows Script to Recursively Copy Files and add directory name prefix - windows

I'm need a script to recursively copy a set of files from a folder and all the subfolders to a new folder. I do not want the directory structure copied. But I want to add the original directory name (not the full path) as a prefix to the copied file.
For example...copy
c:/photos/date1/photo1.jpg
c:/photos/date1/photo2.jpg
c:/photos/date1/photo3.jpg
c:/photos/date2/photo1.jpg
c:/photos/date2/photo2.jpg
c:/photos/date2/photo3.jpg
to:
c:/newfolder/date1-photo1.jpg
.....date1-photo2.jpg
.....date1-photo3.jpg
.....date2-photo1.jpg
.....date2-photo2.jpg
.....date2-photo3.jpg
I have this for loop to do the original copy which works just fine. But I am stuck at how to add the directory name as a prefix.
for /r %d in (*) do copy "%d" "x:\newfolder"
Appreciate advise/suggestions.

Related

How to get folder path from file path with CMD and change for new similar path?

I see this question How to get folder path from file path with CMD
and try resolve my problem without success.
I need move files from origem to destination similar path using CMD
my source with subfolder "origem"
c:\temp\origem\subfolderA\file1.pdf
c:\temp\origem\subfolderA\file2.pdf
c:\temp\origem\subfolderA\file3.pdf
c:\temp\origem\subfolderB\file1.pdf
I need move files to diferent (similar) path with changing subfolder "origem" to "destination"
c:\temp\destination\subfolderA\file1.pdf
c:\temp\destination\subfolderA\file2.pdf
c:\temp\destination\subfolderA\file3.pdf
c:\temp\destination\subfolderB\file1.pdf
If use above code
FOR /R c:\temp\origem %F IN (*.pdf) DO move "%~dpfF" "%~dpF"
On "%~dpF" the parameter p I don't know change to destination

Get file names with complete path in a single txt file using shell script || windows

Resources:
So I have multiple folders ordered/sorted by date (could be any other parameter as well)
inside each folder there are sub folders named as numbers and inside each folder there is a "single" test.css file (extension is constant name can be different i.e all files are .css).
so for example sample folder structure can be something like this
- project/05.09.2010/00/test.css
- project/05.09.2010/01/test.css
- project/05.09.2010/03/test.css
- project/05.09.2010/09/test.css
- project/07.10.2013/01/test.css
- project/07.10.2013/05/test.css
Requirement: I want to loop through all folders in order of date and folder number and get list of .css file name in a txt file.
for e.g for above case my output should be:
file 'project/05.09.2010/00/test.css'
file 'project/05.09.2010/01/test.css'
file 'project/05.09.2010/03/test.css'
file 'project/05.09.2010/09/test.css'
file 'project/07.10.2013/01/test.css'
file 'project/07.10.2013/05/test.css'
I will be performing certain task on those css files later in the same script.
I need code to run in windows batch file.
I am new to shell scripting and have no idea how to approach this scenario.
As #Stephan stated, dir /s /b "*.css" will work. However, it does not output it into a text file. This should work:
cd "C:\parent directory"
rem set parent directory to the parent directory
dir /s /b "*.css" > "%userprofile%\Desktop\cssfiles.txt
This will find all of the .css files and put their names into a text file on your desktop.

Recusively copy files of a specific pattern and recreate the folders hierarchy

How can I copy a set of files by a specific pattern from a set of deeply structured folders recursively into another folder? Also I need to recreate the folder hierarchy from source folder in the target folder (only that folders, which contain copied files). I need to use standard Windows command-line tools.
This question looks like this one: How can I recursively copy files of a specific pattern into a single flat folder on Windows? ; but in my case I want to keep folder structure, so this script will not do this:
for /r %x in (*.dll, *pdb) do copy "%x" targetDir\
The decision is:
FOR /r %x in (PATTERN) DO
(if not exist TARGET_DIR%~px mkdir TARGET_DIR%~px) & (copy %~x c:\\TARGET_DIR%~px)
So, the "secret" is in %~px command which gives relative path of copied file, so we should create this relative path in target dir.

Moving files with specific file name & extension

I'd like to create a batch file under windows to move files with specific file names. I'd like to move all the files with txt extension and filename starting with "HH", and moving them only from root, sub directories excluded. And if a file with the same name is already exist in the destination directory I'd like to auto rename files instead of overwriting. Is it possible to do?
You can simply use:
move c:\HH*.txt destination_directory

Copy single file instead of entire directory in windows batch

Let's suppose that I am in some directory with two subdirectories, a and b. a has two files in it: t1.txt and t2.txt. That is, I have the following directory structure:
/.
/a
t1.txt
t2.txt
/b
I want to copy the file t1.txt from the a directory into the b directory.
I tried the following command
copy /b a/t1.txt b/t1.txt
but it copies the entire a directory into the b directory.
Why does this happen, and how can I make it so that only the t1.txt file is copied?
When copying to a new directory, you only need to specify the new directory. So
copy /b a\t1.txt b
should work.
That said, I don't think additionally specifying the file name would cause the error you've described -- the official help text says "Destination can consist of a drive letter and colon, a folder name, a file name, or a combination of these," which to me implies that how you have it is fine.
I've also reversed the slashes -- were you using forward slashes in your batch file or is that a typo in the post? Maybe that was the problem?

Resources