So I am trying to code an "A.I." through batch. The user will be able to enter in information, and the program will track keywords, and compile all data onto one text file in a specific folder. I have almost everything but a line that searches for all the filenames with a certain keyword. Here is an example. User: "Tell me EVERYTHING you know about GEORGE WASHINGTON" Program: Searches through memory for folder "George Washington" then gathers all the .txt files from that folder. It then types everything from those files to the user. Pls help, thanks :-)
you can do:
#echo off
set "foldertosearch=George Washington"
cd C:\
for /d /r %%a in ("%fodertosearch%") do (
echo found folder: %%a
cd %%a
)
for /f %%b in (*.txt) do (
echo found file: %%b
type "%%b"
)
pause
i recommend you to change the C:\ of the 3rd line to a specific folder because i am not shure the for /d loop can handle a whole drive. use the where command instead.
Related
First of all, I am a total beginner. I was trying an ultimate script bat file solution for a game. To not annoy you with details, let me tell you what I tried to do.
Example:
I have 17 files. 10 of them are .jpg and 7 of them are .obj files.
The images are in path \mods\images and the objects are in path \mods\models.
I want to list all 17 missing files in a list.txt
The bat file will read that list, search for the files and paste them into the TTSmissing folder
and here are my problems:
The bat script only looks exactly into the source path, but not into subfolders (that's why I wrote \mods\images\, to test if it works) so
what I basically want is: \Tabletop Simulator\Mods\ as source path and
the script shall look into all subfolders, too.
The list.txt only works, when the filenames also have their extensions. is it possible to change the script so i don't need the extension? so it will only look for names and copy the files? (example: the names in the list have to be like: hello123.jpg. It's not working when its only hello123.)
How do I need to change the bat script if i don't want a list.txt but just put the list of files right into the bat file?
#echo off
mkdir %USERPROFILE%\Desktop\TTSmissing
set src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods\Images
set dst_folder=%USERPROFILE%\Desktop\TTSmissing
set file_list=%USERPROFILE%\Desktop\list.txt
for /f "tokens=*" %%i in (%file_list%) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
pause
#echo off
setlocal
set "src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods"
set "dst_folder=%USERPROFILE%\Desktop\TTSmissing"
set "file_list=%USERPROFILE%\Desktop\list.txt"
set "ext_list=.gif .jpeg .jpg .mp4 .obj .pdf .png .webm"
if not exist "%dst_folder%" md "%dst_folder%"
for /d /r "%src_folder%\" %%A in (*) do (
pushd "%%~A" && (
for /f "usebackq delims=" %%B in ("%file_list%") do (
for %%C in (%ext_list%) do (
if exist "%%~B%%~C" (
echo copy /y "%%~B%%~C" "%dst_folder%\"
)
)
)
popd
)
)
You just want to copy files so copy is easier to use than xcopy. The code will echo the copy command to test whether it is working how you want it. If satisfied, remove the echo in front of copy and run the code again to do the actual copy process.
A for /d /r loop will recursively iterate the subdirectories in %src_folder%. pushd will change the current directory to each subdirectory so as can work relative to the source files.
The for /f loop will iterate each line from %file_list%. The simple for loop will iterate each of %ext_list%. If current "name.extension" exists, it will be copied to %dst_folder%.
If you set variables names in a script, it is usually a good idea to use setlocal to keep the variables defined local to the script.
To view help for a command, use command /?. This will work for many of commands used in the code.
View command /? help for copy, for, if, setlocal ...
I want to zip everything in a folder, EVERYTHING, but into individually named archives. For some reason every solution on the internet only zips folders, or fails to work at all.
Currently, I have
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%X.zip" "%%X\"
Which I interpret to mean
for = initiate a loop
/d = I don't know what this means
%%X = I don't know what this means
in = not sure, I think it means current directory
(*) = I don't know what this means
do = execute the next thing in "..."
"C:\Program Files\7-Zip\7z.exe" = the thing I want done.
a = add to archive
-m9 = max compression
"%%X.zip" = make it a zip file, though I still don't know what %%X is.
"%%X\" = even if I knew what "%%X\" meant I don't know why it's here.
I have figured out replacing %%X gives the archive a name, so it seems to copy the name of the thing being targeted.
So if I guess, I think /d is "target folders" and %%X is the name.
So
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%X.zip" "%%X\"
Says in English as I understand it: for every folder name in the current directory, use 7z to max compress it into a zip of the same name... except I don't know what to replace /d with to make it target files instead of folders. And targeting specific extensions would be even better.
I tried googling what does "/d in cmd mean," "what does %%X mean", etc. I don't seem to be getting correct results in the search, I think I'm being too vague for google.
UPDATE:
for %%i in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%i.zip" "%%i\*.*"
seems to make a zip file NAMED each item in the folder, but does not actually add any files to them. I tried adding /f, but it didn't work at all when I did that.
Additionally, the first time I posted this it was closed as a duplicate of Batch script loop which has almost nothing to do with my problem. Yes, I have a loop, yes that addresses batch loops, but no, it does not come close to solving my problem since my problem isn't with the loop itself., or if it does I have absolutely no idea why or how. So please, explain it to me. I did see the section where it says %%X is the variable, but that just means I suppose X could be anything I want it to be, and since in my update I indicated a secondary issue, I think the problem I'm having is with 7z and not the bat file.
The for command can be very confusing at first, however there is many videos and sites that show examples of how to use it.
To start with try for /? to see the built in help information.
The below code should be what you are after if i understand your requirements correctly.. if not then this should help you understand the principles and adjust for your needs.
for /F "tokens=*" %%a in ('DIR /A-D /B /ON "C:\Test Folder\"') do (
echo Processing File: C:\Test Folder\%%a
"C:\Program Files\7-Zip\7z.exe" a -mx1 -tzip "C:\Test Folder\%%a.zip" "C:\Test Folder\%%a"
echo.
)
pause
Now lets go through it line by line...
for /F "tokens=*" %%a in ('DIR /A-D /B /ON "C:\Test Folder\"') do (
/F Tells it to work with files instead of directories(/D)
in (' ') Contains the command that finds the filenames. Note the ' marks at each end of the command...they are important.
%%a Represents each value of a filename that the in() command passes it.
do ( ) What it should do with each %%a value it receives.
Now we get to the command that is finding the filenames and passing to the do () section as %%a
('DIR /A-D /B /ON "C:\Test Folder\"')
You can find more info on the this by running dir /?
/A-D Tells it to ignore Directories... only list files.
/B Output just the file names and not anything else like sizes or it will confuse the for command.
/ON Feed the filenames to the do() command in alphabetical order
"C:\Test Folder\" The folder that contains all the files we want to list/perform actions on. Make sure you include the trailing slash. Also use the "" marks or it wont work if a folder has a space in its name.
Write(echo) to the screen a message showing which file it is about to do.
echo Processing File: C:\Test Folder\%%a
You can find more information on how to use the 7zip command line functions at https://sevenzip.osdn.jp/chm/cmdline/syntax.htm
"C:\Program Files\7-Zip\7z.exe" a -mx1 -tzip "C:\Test Folder\%%a.zip" "C:\Test Folder\%%a"
"C:\Program Files\7-Zip\7z.exe" Run this program.... and pass it the following information (arguments/switches)
a Add files rather than extract, etc.
-mx1 Use the fastest compression method...but less space efficient.
-tzip Use the ZIP compression format
"C:\Test Folder\%%a.zip" Save the new "zip" file as this. (%%a will be replaced with the name of the current file... aka ChuckNorris.mpg which would end up as ChuckNorris.mpg.zip)
"C:\Test Folder\%%a" This is the actual file you want to zip.
I really need a batch file to delete the quality, sound profile, and video codec from the folder name..I was thinking a batch file that removes anything after ")" character or delete certain words from folders only.
I have 3000 movies and this would help me soooo much.
I have a bunch of folders named like this
Example:
Carnage Park (2016) 1080p 5.1 x264
Batch file result:
Carnage Park (2016)
p.s
I have searched the site but all I find is ways to rename or delete file names and not folders.
Thanks for the help guys, your the best.
To avoid errors when some matching dirs are stacked I reverse sort the dir list.
#Echo off
Cd /D "X:\start\in\folder"
For /f "delims=" %%A in (
'Dir /B/S/AD "*(*)*" ^|Sort /R'
) Do For /f "tokens=1,2 delims=()" %%B in (
"%%A"
) Do Echo Ren "%%~A" "%%B(%%C)"
If the output looks OK, remove the echo in the last line.
Sample output:
Ren "C:\Test\blah (2010) blubb" "C:\Test\blah (2010)"
I would like to append my folder name to all the available .txt files inside a subfolder. Below is the file/directory structure. I need to achieve this in Windows BATCH script.
C:\Source\Source1\1\a.txt C:\Source\Source1\1\b.txt
C:\Source\Source1\2\a.txt C:\Source\Source1\2\b.txt
C:\Source\Source2\3\a.txt C:\Source\Source2\3\b.txt
The above files should be renamed like below:
C:\Source\Source1\1\1_a.txt C:\Source\Source1\1\1_b.txt
C:\Source\Source1\2\2_a.txt C:\Source\Source1\2\2_b.txt
C:\Source\Source2\3\3_a.txt C:\Source\Source2\3\3_b.txt
Similary, I have Source1...Source30 and under each source directory, I will have multiple folders with different numbers. I need to rename all the files under these directories and append the number(directory name) to the file name.
So far below is what I wrote:
for %%* in (.) do set CurrDirName=%%~nx*
echo %CurrDirName%
for /r %%x in (*.txt) do ren "%%x" "%CurrDirName%_%%x"
With this, I am able to achieve it in a single directory. I couldn't make it recursive. Could you guys please help me with this.
#echo OFF
SETLOCAL EnableExtensions
for /F "delims=" %%G in ('dir /B /S "C:\Source\*.txt"') do (
for %%g in ("%%~dpG.") do ECHO rename "%%~fG" "%%~nxg_%%~nxG"
)
pause
where the FOR loops are:
outer %%G loop creates a static list of .txt files (recursively), and
inner %%g loop gets the parent folder of every particular file.
The rename command is merely displayed using ECHO for debugging purposes. To make it operational, remove word ECHO (no sooner than debugged).
Moreover, I'd consider checking whether a particular file is already renamed…
I am trying to find a way to create a Windows batch script that will look at a target folder full of .pdf files and move (or copy) them to another directory with existing subfolders based on the filename.
The files and folders are names of actual people. I want to be able to get that person's pdf into their existing folder using a script.
Say I have 2 files in my folder; smithJohn015.pdf and thomasBill030.pdf.
I would like to be able to put smithJohn015.pdf into folder SmithJohn and thomasBill030.pdf into folder ThomasBill.
I don't want the script to create new folders or overwrite existing files if there's a duplicate filename.
I'm not necessarily looking for anyone to write a script for me, but if anyone can just get me started in the right direction it would be appreciated.
Try modifying this answer for your evil purposes.
#echo off
setlocal
pushd "c:\path\to\PDFs"
for /d %%I in (c:\Path\To\People\*) do (
for %%F in (*) do (
for /f %%A in ('echo %%~nF ^| find /i "%%~nI"') do (
set /p a="Moving %%F to %%I... "<NUL
move "%%F" "%%I" >NUL
echo Done.
)
)
)
popd
You'll need to add a check for if not exist pdffile before the move, but there's a starting direction for you anyway.
The following assumes the target subfolders' location contains only the subfolders where the PDFs may go and that every PDF that you want to move has a name formatted as the corresponding subfolder's name followed by exactly three characters (same as in your examples):
#ECHO OFF
FOR /D %%D IN ("D:\path\to\subfolders\*") DO (
MOVE "D:\path\to\PDFs\%%~nD???.pdf" "%%D"
)
Or as a one-liner to execute directly at the command prompt:
FOR /D %D IN ("D:\path\to\subfolders\*") DO (MOVE "D:\path\to\PDFs\%~nD???.pdf" "%D")
folderlist.txt contains all names of folders in which you want to copy respective PDFs.
xcopy is copy command. format xcopy "source" "destination" /parameters.
pause is just to keep command window on
for /F "tokens=*" %%A in (folderlist.txt) do (
xcopy "E:\path\to\source folder\<prefix>%%A<suffix>.pdf" "E:\path\to\destination folder\<prefix>%%A<suffix>\" /s)
pause
You can use wildcards in source & destination paths.