How to loop through folders to find the file and move the file to other folder - windows-shell

I want to move the files to the respective folders using batch file.
Someone please help as i am new to scripting.
I have folder hiererchy as below
Root
Root1
Doc2012.txt
Doc2013.txt
Doc2012.txt
Root2
Doc2012.txt
Doc2013.txt
Doc2012.txt
Root3
Doc2012.txt
Doc2013.txt
Doc2012.txt
Then i have another 2 folder 2012,2013..i want to move the file from root1,root2,root3 which contains 2012 in their name to folder->2012 and the file which contains 2013 in their names to Folder->2013

Try following:
create file move2012.cmd in the root of your hiererchy,
put this line in move2012.cmd (it moves all files with '2012' in name to directory 2012):
for /r . %%i in (*2012*) do move %%i 2012
run move2012.cmd
If it is what you want, now it is easy to extend this cmd file for 2011, 2013 etc.

Related

Windows Script to Recursively Copy Files and add directory name prefix

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.

I'm looking for a script that will rename files and place them in a new folder

Essentially what I am looking to create is a script that will rename files in a folder, create a new folder with a specific name and place the renamed file in that new folder.
So, for instance, let's say that I had 2 files called:
test-spa.txt
test-ger.txt
I would then want to create 2 folders called spa and ger, respectively, place the appropriate file into each folder then rename the file by removing the language component; the resulting files in each folder would be test.txt.
Thanks,
Jaime
So here is the simple solution I came up with to create folders and place specific files in them. So long as I have the bat file in the same folder it works great:
#echo off
md spa ger
move file.txt EN\file.txt
move file-spa.txt spa\file.txt
move file-ger.txt ger\file.txt
I'm wondering if there is something missing that may cause an issue, such as specifying that this should only work in the current directory?

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.

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