copy file based on content - command prompt - windows

I got a folder with bunch of files in it. each file name has unique date ex: 20140101. each file has data of one product.. ex: 20140101 has data for product "oranges" file 20140102 has data of product "apple" .. I need to create a script which will check the content of file and if match found, copy the file to different directory.
Example:
Find "Oranges" in
C:\Data\
found 2 files .. 20131229 and 20140101
copy files to c:\oranges\
Thanks in advance.

#echo off
setlocal enableextensions disabledelayedexpansion
set "source=c:\data\*.*"
set "target=c:\"
set "classes=orange apple"
for %%c in (%classes%) do (
if not exist "%target%%%c\" md "%target%%%c\"
for /f "delims=" %%f in ('findstr /m /l /c:"%%c" "%source%"') do (
copy "%%~ff" "%target%%%c\"
)
)
For each of the indicated classes, create target directory if it does not exist, and for each file with the indicated content, move it to the adecuated directory (not tested, adapt as needed)

Related

Create Folders and Relocate Files Based on Partial Filename

I have a series of photos in a folder with name format similar to this:
BA-ML-6256_Gocchup1.jpg
BA-ML-6256_Gocchup2.jpg
BA-ML-17302_Gocchup1.jpg
BA-ML-17302_Gocchup2.jpg
I want to create new folders like below that contain the files:
BA-ML-6256
BA-ML-17302
I tried using this script:
#echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)
but it created these 4 folders instead:
BA-ML-6256_Gocchup1
BA-ML-6256_Gocchup2
BA-ML-17302_Gocchup1
BA-ML-17302_Gocchup2
Please help me create a batch script that would make this work.
#ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
FOR /f "tokens=1*delims=_" %%b IN ('dir /b /a-d "%sourcedir%\*_*.jpg"') DO (
ECHO MD "%sourcedir%\%%b"
ECHO MOVE "%sourcedir%\%%b_%%c" "%sourcedir%\%%b"
)
GOTO :eof
Always verify against a test directory before applying to real data.
The required MD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
Naturally, you could remove the %sourcedir%\ throughout if you want to process the current directory.
The dir command lists the filenames only in the directory, and then only those that match *_*.jpg, that is anything1_anything2.jpg
The for /f assigns anything1 to %%b and anything2.jpg to %%c, using the _ as a delimiter (see for /? from the prompt, or thousands of items on SO for examples/documentation)
Then make the directory and move the file.
Simples!
You need to parse the file names to derive the target folder name.
Sample Batch File
The following batch file should do as you desire:
#echo off
for %%i in (*.jpg) do (
for /f "delims=_" %%j in (%%i) do (
md %%j
move "%%~i" "%%j"
)
)
)
This splits the file name at the "_" character to derive the target folder
Example Output
I started with this folder layout:
F:\projects\sx\batch>tree /f
Folder PATH listing
Volume serial number is 9C33-6BBD
F:.
create-test-data.cmd
md-and-move.cmd
BA-ML-6256_Gocchup1.jpg
BA-ML-6256_Gocchup2.jpg
BA-ML-17302_Gocchup1.jpg
BA-ML-17302_Gocchup2.jpg
No subfolders exist
Running the batch file gives this output:
F:\projects\sx\batch>md-and-move.cmd
1 file(s) moved.
A subdirectory or file BA-ML-6256 already exists.
1 file(s) moved.
1 file(s) moved.
A subdirectory or file BA-ML-17302 already exists.
1 file(s) moved.
and results in this folder layout:
F:\projects\sx\batch>tree /f
Folder PATH listing
Volume serial number is 9C33-6BBD
F:.
│ create-test-data.cmd
│ md-and-move.cmd
│
├───BA-ML-6256
│ BA-ML-6256_Gocchup1.jpg
│ BA-ML-6256_Gocchup2.jpg
│
└───BA-ML-17302
BA-ML-17302_Gocchup1.jpg
BA-ML-17302_Gocchup2.jpg

Move files into folders in batches of five?

Suppose I have 50 files sorted by name.. I would like to create a batch script which gives me the following result:
Files 1 through 5 -> 01-05
Files 6 through 10 -> 06-10
and so on..How can I create a batch script to achieve this?
Note that 01-05 and 06-10 are directory names..
EDIT: Details
For eg. Consider this:
Source Directory:
101.mp4
102.mp4
103.mp4
104.mp4
and so on..
I want a resulting directory structure like this:
Destination Directory:
101-105:
101.mp4
102.mp4
103.mp4
104.mp4
105.mp4
106-110:
106.mp4
107.mp4
108.mp4
109.mp4
110.mp4
and so on..
This is what you want, change fileCount to change the file number in each subdirectory:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set fileCount=5
set filesNow=0
set nameStart=000
set nameEnd=000
FOR /F "usebackq delims=" %%i IN (`dir /b /O:N *.mp4`) do (
set /a filesNow+=1
set /a tmpValue=filesNow %% fileCount
::echo !filesNow!
::echo !fileCount!
::echo %%i
::echo !tmpValue!
if "!tmpValue!"=="1" (
set "nameStart=%%~ni"
mkdir _tmpDir_
)
move %%~nxi _tmpDir_\
if "!tmpValue!"=="0" (
rename _tmpDir_ !nameStart!-%%~ni
)
set "nameEnd=%%~ni"
)
if exist _tmpDir_ rename _tmpDir_ %nameStart%-%nameEnd%
You need to put them inside a bat/cmd file to work.
filesNow is for file number count.
Basically it's create a tmp folder and move files inside,
When files inside it reach the number, change the folder's name.
Several testing echo command I didn't remove, just used :: to comment them out, you can remove the :: to test them again.

Bat script to find and replace files in multiple subfolders - replace .java files with .class files from specific folder

I am new to windows batch scripting .. please help on this scenario
I have file structures as below ::
dir1:
c:\workspace\changeset\com\folder
subfolder1
one.java
subfolder-2
two.java
dir2:
c:\workspace\target\class\com\folder
subfolder1
one.class
subfolder2
two.class
Subfolder3
three.class
Need to find and replace dir1 files in respective subfolders i.e one.java and two.java from dir2 files i.e one.class and two.class ( need to replace certain .java files with .class files from specific folder )
much appreciated for your help.
Thanks
Arjun
for /f "delims=" %%a in ('dir /b /a-d "c:\workspace\changeset\com\folder\*.java"') do (
if exist "c:\workspace\target\class\com\folder\%%~na.class" (
echo copy "c:\workspace\target\class\com\folder\%%~na.class" "c:\workspace\changeset\com\folder\%%a")
)
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files. Append >nul to suppress report messages (eg. 1 file copied)
Note that execution directly from the prompt and as lines with a batch file are different. The metavariable (loop-control variable) %%x must be referenced as %%x for a batch line and %x if executed from the command prompt. Since it makes little sense to repeatedly execute a line containing a for from the prompt (it's easier to create a batch file), I post the batch-file version. User's responsibility to adjust for direct-from-prompt if desired.
Read each filename in /b basic form /a-d without directories and assign the filename+extension to %%a.
If a file in the other directory called thenamepartofthefile.class exists, then copy that file to the first directory.
Please post the entire problem to be solved. The approach can change radically, as in this case.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir1=U:\sourcedir\changeset"
SET "sourcedir2=U:\sourcedir\target"
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%sourcedir1%\*.java" '
) DO (
SET "javadir=%%~dpa"
SET "classfile=!javadir:%sourcedir1%=%sourcedir2%!%%~na.class"
IF EXIST !classfile! ECHO COPY "!classfile!" "%%a"
)
GOTO :EOF
You would need to change the settings of sourcedir1 and sourcedir2 to suit your circumstances.
Essentially, the same approach and the same comments re messages. The difference is that this procedure uses files and subdirectories in the dir list and substitutes the first part of the directoryname in deriving the expected name of the .class file.

How to create folders and files from text file with same names to insert with corresponding name?

I was inspired on works of our colleagues from portal
Have a batch create text files based on a list
Batch script to read input text file and make text file for each line of input text file
to develop creation from a text file for example
"comps.txt" ( comp1,comp2....
for each PC a batch script read from a list of PCs in a text file and create a text file for each line of text as local.
Later in code are created folders files named - the same names ( comp1,comp2....
at the end we have text files : comp1.txt ,comp2.txt ... and folders: comp1 , comp2.... till 400 computers.
Any idea how to add in code or write another separate batch code to move for each coresponding folder text file for text file
comp1.txt->comp1 folder
comp2.txt->comp2.txt
....
We have more than 400 lines!!
I am very begginers for any scrips working very hard every single day and this is my first question. My code in Windows Batch is below
#echo off
setlocal
for /f "tokens=*" %%a in (comps.txt) do (type nul>"%%a.txt")
for /f "tokens=*" %%a in (comps.txt) do (
echo This is line 1 of text>"%%a.txt"
)
for /f %%i in (comps.txt) do mkdir %%i
do (
echo "%%i.txt"
)
endlocal
[]
It doesn't make any sense to generate text files and then folders to move the files to. Create the folders first place and create the files into the folders. Using a (code block) only one for loop is needed.
#echo off
setlocal
for /f "tokens=*" %%a in (comps.txt) do (
if not exist "%%a" mkdir "%%a"
echo This is line 1 of text>"%%a\%%a.txt"
)
endlocal

Put files automatically in folders

I have thousands of JPGs named like this "aaa0001.jpg, aaa0002.jpg, aaa0003.jpg,
bbb0001.jpg, bbb0002.jpg, bbb0003.jpg, ccc0001.jpg, ccc0002.jpg, ccc0003.jpg etc." in one folder.
I have created 26 folders like this aaa, bbb, ccc, ddd etc.
Is it possible to create a script that sets all the images in the appropriate folder?
Result "aaa0001.jpg, aaa0002.jpg, aaa0003.jpg" into folder "aaa",
"bbb0001.jpg, bbb0002.jpg, bbb0003.jpg" into folder "bbb" etc.
Thank you!
My system is windows XP prof SP3...
It would go like this in a Windows/dos batch file.
The statement %fp:~0,3% determines which part of the filename is used as a foldername. 0,3 means: from the first character and the next 3 chars.
so a file named aaa001-01.jpg will give a folder of aaa.
To have files named abc001_03.jpg go into folder 001 you change the statement to %fp:~3,3%
for %%a in (*.jpg) do call :copyfile %%a
goto :eof
:copyfile
set fp=%1
set folder=%fp:~0,3%
rem remove echo on the next line...
echo copy "%1" "%folder%"
rem or for moving: move /Y "%1" "%folder%"
goto :eof
Just define the base path to create the news directorys in the VAR $path
#echo off
setlocal EnableDelayedExpansion
:::The path where the new Directorys will bw created
set $path="c:\Image\"
for %%a in (*.jpg) do (set $file="%%a"
set $Dir="%$path%CSV!$file:~4,3!"
if not exist "!$dir!" md "!$dir!"
move "!$file!" "!$dir!")
echo Terminated

Resources