Batch script to move files that match another set of files? [closed] - windows

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
This would help me a lot of possible.
Example:
Folder1 has 900 files ending in .zip.
Folder2 has 300 files that match the ones in Folder1 but in .doc
How can I create a batch script to move files from Folder1 to Folder1-sorted that only have a .doc alternative ending up with a folder1-sorted with only 300 .zip files?
My initial guess was doing dir > list.txt and changing the extension of every line and using this list to copy.. but I guess there might be a more clever solution to doing this.

With the aid of Setlocal EnableDelayedExpansion and For loops you could build an indexed array whose values contain just the filenames for your .doc files, using the ~n variable modifier, then use another for loop on your .zip files to compare against the array with a nested For /L loop.
EG:
#ECHO OFF & Setlocal EnableDelayedExpansion
CD "your path for parent directory containing folders 1 and 2"
REM build the array using the smaller data set...
PUSHD Folder2
Set "_I=0"
For %%A in (*.doc) Do (
Set /A _I+=1
Set "file[!_I!]=%%~nA"
)
POPD
PUSHD Folder1
For %%A in (*.zip) Do (
For /L %%B in (1,1,!_I!) Do (
If "!file[%%B]!"=="%%~nA" (
ECHO(%%A matches !file[%%B]!.doc
REM command for on Match here
)
)
)
POPD

Related

how to write batch script on command prompt to extract nth line from multiple txt files? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
What's the batch script I should write on Windows command prompt to extract the values from 20th line of 200 text files and put them into a new text file named "master.txt"
Each text files contains hundreds of lines, and I only need the value from line 20.
For example
text file name:
test1.edit.txt
test2.edit.txt
test3.edit.txt
......
Sample File:
Line 1 - xxxxx
Line 2 - xxxxx ......
Line 20- PROPERTY 100
Result:
PROPERTY 100 (from test1.edit.txt)
PROPERTY 200 (from test2.edit.txt)
PROPERTY 300 (from test3.edit.txt) ......
Thank you in advance
If you have cigwin installed, write a,simple bash script
that looks like:
for f in `ls f?`; do
head -9 $f | tail -1
done
change the "f?" to find whatever your file names are (mine
are f1, f2, f3). Save the script as "line9" and run it as
bash line9
If you don't have cygwin, consider installing it. :-)
It really does have lots of useful unixish tools.
Copy this code to a batch file. Drag the folder where the TXT files are to the batch file. It should save all "line 20" text to a file called "Result.txt"
#echo off
if /i exist "%~1" (if /i not exist "%~1\" exit) else (exit)
set "Folder=%~1"
if /i exist "Result.txt" del /q "Result.txt"
pushd "%Folder%"
for /f "delims=" %%a in ('dir /b *.txt') do Call :GetLine "%%a"
popd
cmd.exe /c start "" notepad Result.txt
exit
:GetLine
for /f "skip=19 eol= delims=" %%a in ('type "%~1"') do (
echo %%a
>>%~dp0Result.txt echo %%a
goto :EOF
)
goto :EOF

Script to list extensions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to write a windows batch script that lists all extensions in a directory, recursively.
The desired behaviour is this:
You start the bat file in directory "a" which contains the following files: b.txt, c.png, d.txt, e.jpeg, f.jpg.
The script should output the following:
txt
png
jpeg
jpg
I would like the each extension to be in a new line, alphabetic ordering is not necessary.
The method you linked to is probably overkill for what you want.
Do a for /r loop to get all files recursively and set a variable named the same as the extension (the value isn't important, we need only the variable to be defined)
Then do a for /f loop to list the variables (with set . - all of them start with a dot conveniently because the %%~x modifier gets the extension with the dot (.txt)). Then just sort them alphabetically, if needed:
#echo off & setlocal
for /r %%a in (*) do set "%%~xa=X" 2>nul
for /f "delims==" %%a in ('set .') do echo %%a
(edit: removed the sort, because set . already sorts alphabetically - thank you #LotPings for the hint)

Rename large amount of files using a counter loop within subfolders with a batch file - Windows 7 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In Windows 7, I have a folder which only contains subfolders and no files. Within the subfolders, I have a bunch of .dcm files (medical images). The files within each subfolder have the same name, and I want them to all have different names, more specifically, I want the names to be order 1.dcm, 2.dcm, 3.dcm, etc.
Current situation:
Folders A, B, and C, and each folder contains File1.dcm, File2.dec, File3.dcm
Would like to have this:
Folder A contains files 1.dcm, 2.dcm, 3.dcm
Folder B contains files 4.dcm, 5.dcm, 6.dcm
Folder C contains files 7.dcm, 8.dcm, 9.dcm
Is there a way to write a batch file that goes into each subfolder in order and renames the files in numerical order? I am very much the novice when it comes to writing script for Windows and have no idea how to go about doing this. I found code that changes file extensions within subfolders, but nothing that does what I am looking for.
Any help is greatly appreciated!
#Echo off
For /f "tokens=1* delims=:" %%A in (
'Dir /B/S/ON/A-D "X:\path\to\folder\*.dcm" ^|findstr /N /V "^$"'
) Do Echo Ren "%%~fB" "%%A%%~xB"
If the output seems ok remove the echo in the last line.
If you have numbers with different length in one folder be aware that sorting by name will have 10 before 2.
For better understanding some explanations:
Dir /B/S/ON/A-D "X:\path\to\folder*.dcm"
/Bare format
/Subdtree recurse
/Order by Name
/Attribute -(not) Directory
Findstr /N /V "^$"
/Number each output line delimit with a colon.
/inVerse the match
"^$" a Regular Expression meaning an empty line
^ matches the begin of a line
$ matches the end of a line
For /f "tokens=1* delims=:" %%A in ('command') do
Parses output of 'comand' into tokens 1=%%A and *=rest into %%B
the delimiter : is only used for token 1,
token 2 takes the rest of the line (important to preserve the drive colon)
The vars %%A and %%B are passed to the do (part)
The for vars can be modfied with ~ modifiers see For /?
%%~fB ~f meaning fullname including dirve and path,
%%A%%~xB ~x is the extension from %%~xB including the dot

Creating year,month,date folder structure based on filename [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have some files in a some unix server. When i copy to windows, based on filename , the file has to be copied into corresponding year,corresponding month and corresponding date folders. Sample filename : 20120201.117_visual_sciences_web_feed.out.gz.
Folder structure to be created based on first part of filename, in this case : 20120201(YYYY,MM,DD) . In above example ,filename should be copied into 2012 -> 02-> 01 folder.Folders should be created if not created Honestly i am not getting how this can be implemented, please suggest.
This should do the trick for you -
#echo off
set filename=20120201.117_visual_sciences_web_feed.out.gz
set filepath=PATH\TO\%filename%
REM get the date from the file name
for /f "delims=." %%A in ("%filename%") do set fdate=%%A
REM Y=YEAR; M=MONTH; D=DAY
set Y=%fdate:~0,4%
set M=%fdate:~4,2%
set D=%fdate:~6,2%
REM echo %Y% %M% %D%
REM check to see if directories exist
if not exist \%Y% mkdir %Y%
if not exist \%Y%\%M% mkdir %Y%\%M%
if not exist \%Y%\%M%\%D% mkdir %Y%\%M%\%D%
REM copy the file to the newly created destination folder
copy %filepath% %Y%\%M%\%D%\%filename%
pause
I hope you're able to implement this into your script easily enough. As pointed out; if you added some examples from your script - I would be able to help more.

Findstr -- exclude a file type -- search only ascii [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I want to run a FINDSTR dos command in a way beyond what it is easily found in "findstr /?". How would I run findstr so that it only searches ascii files. (I am not sure if that is possible. My gut feeling is that it is not possible) Additionally, how would I run this command line so that it would exclude some file times. For example, what if I wanted to exclude .psd files
What is wrong with the /P option that is described in the help?
/P Skip files with non-printable characters.
It worked for me.
To take further control of which files are searched, simply iterate the files with a for loop and add whatever logic you need. To skip .psd files and also skip binary files:
for /f "eol=: delims=" %%F in ('dir /a-d /b^|findstr /live ".psd"') do findstr /p "search" "%%F"
Use a single percent instead of double percents if run from the command line.

Resources