I need some help with batch files as I have not done it before. I need to find textfiles with a matching pattern and move them to a class folder. I have a couple of folders that I am browsing through. Can anyone help me to modify the code to move the textfiles together?
This is what i research and piece together.
#echo off
setlocal enabledelayedexpansion
set file=name.txt
set foldername=class
set location=blockB
for /f "tokens=* delims=" %%a in (!file!) do (
set folder=%%a\public\
if not exists "%CD%\!folder!\!foldername!" (mkdir "%CD%\!folder!\!foldername!") ELSE (call)
dir /b "!folder!" | findstr /r /c"!location!"
)
my name.txt contains (adam,ben,charlie) 1 name per each line. So technically i want to move text files in (\adam\public) which contains blockB in the textfile name to a newly created folder call class (\adam\public\class). i want it to do the same for (\ben\public) and (\charlie\public). If I have any mistake in the way i code pls pardon me. Thanks.
Reason why I did not use a full path is because I am going to use it on different computers with same folder configurations.
I guess you are trying to accomplish this:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE=name.txt"
set "FOLDERNAME=class"
set "LOCATION=blockB"
for /F "usebackq eol=| delims=" %%F in ("!FILE!") do (
set "FOLDER=%%~fF\public"
mkdir "!FOLDER!\!FOLDERNAME!"
move "!FOLDER!\*!LOCATION!*.txt" "!FOLDER!\!FOLDERNAME!\"
)
endlocal
Related
I have a folder called TEST. Inside there are 30 files.
Example:
DIM1_UPI_20170102.TXT
DIM2_UPI_20170908.TXT
DIM3_UPI_20180101.TXT
...
I have to rename them by removing the date tag
Exapmple:
DIM1_UPI.TXT
DIM2_UPI.TXT
DIM3_UPI.TXT
Can you please help me writing this in batch file?
Assuming your files are all starting with DIM
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir "*.TXT" /b /a-d') do (
set "var=%%~ni"
echo ren !var!%%~xi !var:~0,-9!%%~xi
)
Once you can confirm that it does what you want, and ONLY then, remove the echofrom the last line to actually rename the files.
Important Note. If you have files with similar names, but different date entries, this will not work as you think. as Example:
DIM2_UPI_20170910.TXT
DIM2_UPI_20170908.TXT
The names are the same, but dates differ, making each filename Unique. If you rename them, there can be only 1 DIM2_UPI.TXT So as long as you understand this, you will be fine.
Edit: based on Amazon drive question. Note you need to change the directory portion to how you access amazon drive.
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir "DIM*" /b /a-d') do (
set "var=%%~ni"
echo ren !var!%%~xi !var:~0,-16!%%~xi
)
I am looking for help to construct a .bat script to move PDFs into a predefined folder structure. The file names are structured and relate to where in the folder structure they should be moved to.
For example IRTYCAS001.pdf;
First two letters tell it to move it to the correct country folder (Ireland)
3rd and 4th will tell it what county folder to move it to
5th to 7th will tell it the correct town folder to move it to and
Last 3 digits tell it the land use type folder to move it to
The identifier lengths will always be the same in the file name.
The folder structure looks something like
example of folder structure
With extensions enabled (default) mkdir will create intermediate folders in one step.
So all you've to do is
iterate the files
use substrings to split the filename into parts and
create the folders if not already existing.
#echo off & setlocal EnableDelayedExpansion
set Src=A:\
set Dst=A:\
for /f "delims=" %%A in ('Dir /B "%Src%*.pdf"') do (
Set "File=%%A"
set "Folder=%Dst%\!File:~0,2!\!File:~2,2!\!File:~4,3!\!File:~7,3!\"
if not exist "!Folder!" MD "!Folder!" >NUL
Move "%%A" "!Folder!"
)
Sample tree:
> tree . /f
A:\
└───IR
└───TY
└───CAS
└───001
IRTYCAS001.pdf
you could try something like this:
FOR /F %%i IN ('dir /b c:\temp\*.pdf') DO call :moveFiles %%i
goto :EOF
:moveFiles
set myfile=%1
set part1=%myfile:~0,2%
set part2=%myfile:~2,2%
set part3=%myfile:~4,3%
set part4=%myfile:~7,3%
set dstFolder=C:\temp
if %part1%==IR set dstFolder=%dstFolder%\ireland
REM more options here...
if %part2%==TY set dstFolder=%dstFolder%\tipperary
REM more options here...
if %part3%==CAS set dstFolder=%dstFolder%\cashel
REM more options here...
if %part4%==001 set dstFolder=%dstFolder%\residential
REM more options here...
move /Y %myfile% %dstFolder%
Best Users,
I want to made a batch script that deletes a specific word in a txt file.
I used the following but it didn't work
(Script that I used) :
SET /p word=device
FOR /f "delims=" %%a IN ('dir /a-d /b "Input_%date%.txt"') DO (
SET "fname=%%~na"
SET "fname=!fname:%word%=!"'
IF NOT "!fname!"=="" REN "%%~a" "!fname!%%~xa"
(Input_%date%.txt):
1015faf2da091b02 device
1115fbd4e0dd3b03 device
I want to delete the word "device" in every line.
Can anyone help me out!
Kind Regards,
A.V.R
I guess you want sth like this:
SETLOCAL EnableDelayedExpansion
set x=device
FOR /f "delims=" %%a IN (Input_%date%.txt) DO (
set b=%%a
echo !b:%x%=!
)
But once you install sed (from for example unxutils) you can do this with simple one liner
type input_%date%.txt | sed "s/device//"
I assumed you can forward output to another file afterwards.
I have hundreds of csv files . csv files are stored in folders and sub folders . I want to search fifty csv file whose file names have been determined , for example 1.csv , 2.csv , 3.csv , ... , 50.csv . very troublesome if I searched one by one using the Windows search tool . I would like if the files are found , save in the folder named FOUND . please help to overcome this problem by using the batch programming / bat ? thank you very much
There's a number of approaches one can take, depending on how much automation you require... To help you get started, you may want to look at this it helped me (and indeed continues to do so) when I started learning batch. Furthermore I will provide one possible template for achieving your objective, as I have interpreted it. Perhaps it is not the most elegant or efficient method, but it introduces a number of batch commands that you may or may not have encountered, which in turn may help you develop your own method.
#echo off
setlocal enabledelayedexpansion
echo Please enter a drive letter:
set /p "drive=>"
echo Please enter a search string:
set /p "searchstring=>"
echo %searchstring%>search.txt
set /p search=<search.txt
set /a suffix=0
echo.>>search.txt
:LOOP
for /f "tokens=*" %%i in ("search.txt") do (
set /a suffix=suffix+1
set seq=%search% !suffix!
echo !seq!>>search.txt
)
if !suffix! leq 49 goto LOOP
for /f "tokens=*" %%i in (search.txt) do (
for /f "tokens=*" %%j in ('dir /b /s /a-d %drive%:\"%%i.csv" 2^>nul') do (
if not exist "%~dp0\found" md "%~dp0\found"
move /y "%%j" "%~dp0\found\%%~nxj"
)
)
pause
This is not intended as a definitive solution, though you may find it answers your original query/request. All the best.
Here's another working solution for you..
#ECHO OFF
SETLOCAL EnableDelayedExpansion
REM First Set your directories input and output
SET InputDir=C:\Directory to your CSV files\
SET OutputDir=C:\Directory to your CSV files\FOUND
REM check if the FOUND directory exist, if not, then create it.
IF NOT EXIST OutputDir (
mkdir %OutputDir%
)
REM Grab a scan of the input directory and save it to a temporary file list.
Dir /a /b %InputDir%>"%OutputDir%\Found.txt"
REM Set the files you would like to find.
SET "File1=1.csv"
SET "File2=2.csv"
SET "File3=50.csv"
REM The loop, to process the matching file(s).
FOR %%A IN (%File1%,%File2%,%File3%) DO (
FOR /F "usebackq" %%B IN ("%OutputDir%\Found.txt") DO (
IF %%A==%%B (
copy "%InputDir%\%%A" "%OutputDir%\%%A"
)
)
)
REM Clean up the temp file list.
DEL "%OutputDir%\Found.txt"
Make note, I didn't add quotes to the Input and Output variables, but instead added quotes to the copy portion of the code to compensate for white spaces in your directory path. I tried to keep it simple, so you could follow the logic of how it processed what you are looking for, you can now modify this to your liking.. Have fun. Cheers!
The code below works fine, here is a list of it's functions:
It moves files based on the fist 4 characters to a pre-created folder with the same first 4 characters
If the folder does not exist, it will not move the file, as there is no folder with the same fist 4 chars.
#echo on
setlocal enabledelayedexpansion
cls
pushd R:\Contracts\Sites
for /f "tokens=*" %%1 in ('dir /a-d /b *') do (
set filename=%%1&set dirname=!filename:~0,4!
for /f "tokens=*" %%A in ('dir /ad /b') do (
set dirid=%%A & set dirid=!dirid:~0,4!
if "!dirid!" equ "!dirname!" move %%1 %%A
)
)
I would like to add one extra function to this code please. Pleas have a look at the example below.
I have 5 files
X32A-test.docx or X32A-test.pptx (there will only be one docx or pptx, "NEVER two with the same name")
X32A-test.pdf
X32A-test.avi
X32A-test-eng.sub
X32A-test-small.jpg
I would like the code to CREATE a folder if it does not exist, based on the file name if it has the extension docx or pptx.
So with the above example it would create a folder named: "X32A-test". Then all the other files with "X32A" in the front of the name will be moved to that newly created folder "X32A-test".
I hope it is clear enough. If not please ask me for more information.
Thank you
It is much simpler and more efficient to use the simple FOR instead of FOR /F in your case.
And rather than looping through every file and moving them individually, it is easier and more efficient to use wildcards.
The first loop finds the .pptx and .docx files and creates folders as needed
The second loop finds all the directories and moves all files that start with the directory name into the directory.
#echo on
setlocal enableDelayedExpansion
cls
pushd R:\Contracts\Sites
for %%F in (*.docx *.pptx) do (
set "folder=%%F"
2>nul md !folder:~0,4!
)
for /d %%F in (*) do move %%F* %%F
popd
If needed, you can protect yourself against directory names shorter than length 4.
#echo on
setlocal enableDelayedExpansion
cls
pushd R:\Contracts\Sites
for %%F in (*.docx *.pptx) do (
set "folder=%%F"
set folder=!folder:~0,4!
if !folder:~0,3! neq !folder! 2>nul md !folder!
)
for /d %%F in (????) do (
set "folder=%%F"
if "!folder:~0,3!" neq "%%F" move %%F* %%F
)
popd
Note that this solution may fail if a file name contains !. If that arises then you need to toggle delayed expansion on and off within the loop(s).
I can see the entire process (including the part already implemented) like this:
All the files that are not yet in their "home" directories are moved there.
For all .docx and .pptx files left, create directories based on the files' names.
Obviously, the step #2 creates new "homes" and those will still be "uninhabited" this far. So all that is left to do now is to repeat the step #1.
So I would probably reorganised your process and, with the additional requirement, it could be implemented this way:
…
PUSHD your_root_directory
FOR /D %%D IN (*) DO (
CALL :movefiles "%%D"
)
FOR %%F in (*.docx *.pptx) DO (
MKDIR "%%~dpnF"
CALL :movefiles "%%~dpnF"
)
…
GOTO :EOF
:movefiles
SET "dirname=%~n1"
SET "mask=%dirname:~0,4%*"
MOVE "%~dp1%mask%" %1
Note: The steps #2 and #3 could be either implemented as separate loops or combined in one. The above script uses the latter approach.
You can use negative offsets in the !var:~offset,len! evaluation as follows:
set fspec=X32A-test.docx
echo !fspec:~-10!
echo !fspec:~0,-10!
That second line above gives you -test.docx and you can simply check that against your two desired possibilities with an if statement (or two).
Then you can use the third line to get the rest of the name for creating a directory.
The following example script shows how this could be done:
#setlocal enableextensions enabledelayedexpansion
#echo off
set fspec=X32A-test.docx
set bit1=!fspec:~-10!
set bit2=!fspec:~0,-10!
if .!bit1!.==.-test.docx. echo mkdir !bit2!
if .!bit1!.==.-test.pptx. echo mkdir !bit2!
endlocal
I'm echoing the mkdir command rather than executing it so you need to take out the echo. You'll also need to integrate the set and if statements into your loop but, based on what you have so far, you should have little trouble with that.
If, as you seem to indicate in a comment, the first four characters are the key and the last five decide on whether to make the directory, as in:
x32s-test.docx
a21w-production.pptx
xxxx-whatever_the_blazes_you_want.some_other_rubbish.docx
Then you're really only interested in the first four and last five:
#setlocal enableextensions enabledelayedexpansion
#echo off
set fspec=a12b-whatever_the_blazes_you_want.some_other_rubbish.docx
set bit1=!fspec:~-5!
set bit2=!fspec:~0,4!
if .!bit1!.==..docx. echo mkdir !bit2!
if .!bit1!.==..pptx. echo mkdir !bit2!
endlocal
This checks the correct extensions and outputs:
mkdir a12b
as expected.