I'm currently having to manually edit the batch file to manually tell it which directory to work on.
The directories it has to crawl will ALWAYS start with "2020-" (at least for this year), and sometimes there will be multiple of them if I miss a day.
(if it's any easier, they will ALWAYS be in this format: "0000-00-00")
Is there a way to edit this to account for that?
I've tried just making the source_directory just "2020*" but I know that's probably not how this works lol, any help or pointer in the right direction would be amazing.
#echo off
pushd %~dp0
SET source_directory=2020-05-03
SET target_directory=%~dp0
for /f %%a IN ('dir "%source_directory%" /b') do (
move %source_directory%\%%a %target_directory%
)
Sorry, I don't use SO too often so I forget how things need to be done.
This is the solution I've come up with. I don't think it's the intuitive way of doing this, but I don't understand batch well enough to do this without nesting loops.
#echo off
pushd %~dp0
SET source_directory=????-??-??
SET target_directory=%~dp0
for /d %%s in (%source_directory%) do (
for /f %%a IN ('dir "%%s" /b') do (
move %%s\%%a %target_directory%
)
)
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've looked around, and there are a lot of posts very similar to this but I can't seem to find an answer that will work for what I need.
I have a big list of files with different extensions that I want to rename to numbers. For example
SomeFileName1.jpg
SomeFileName2.gif
SomeFileName3.mkv
changed to:
1.jpg
2.gif
3.mkv
I want to keep the extensions the same. Only the name should change to 1,2,3 etc.
A while ago I found some code that renamed all the files (don't have the code anymore), but it renamed them in the wrong order. "SomeFileName3.mkv" became "1.mkv" instead of "3.mkv" for example. I'm not sure if I need to sort them first somehow? I'm not very good at this kind of thing so I could really use some help. Thanks
use a for loop to process all files. Use dir to sort the files to your needs. Use a counter. Use delayed expansion to make the counter work.
#echo off
setlocal enabledelayedexpansion
set count=0
for /f "delims=" %%a in ('dir /b /on /a-d') do (
set /a count +=1
ECHO ren "%%a" "!count!%%~xa"
)
for more information read for /?, dir /? and set /?
If the output satisfies you, remove the ECHO to arm the ren command.
Bit of a strange thing is happening to me.
I put a renaming batch file together a few years ago (with a lot of help from various places, including StackOverflow) for a project i was working on.
It would rename some files and prefix them with the first 5 characters from the parent folder (i.e. '12345 - Site').
I haven't used the BAT file for a good few months and now have a need to, but it is not working correctly.
It is renaming the files, but it is using the whole of the parent folder instead of just the first 5 characters. I have tested it on another PC and run it in folders where it previously worked.
Does anyone have any ideas as to why this would happen, how to fix it, or what I could add to the batch file to achieve the same result?
Just to point out, I am a complete novice and spent many nights getting the first batch file working through trial and error and cutting and pasting from similar batch file requests on the web.
My current code:
for %%z in ("%cd%") do (
for %%a in ("%%~dpz%\.") do (
for /f "delims=" %%i in ('dir /b /a-d *.pdf,*.xlsx,*.docx,*.xlsm') do move "%%i" "%%~nxz %%i"))
This should work for you.
for %%z in ("%cd%") do (
for %%a in ("%%~dpz%\.") do (
for /f "delims=" %%i in ('dir /b /a-d *.pdf,*.xlsx,*.docx,*.xlsm') do call :renameStuff "%%i" "%%~nxz"
)
)
goto :eof
:renameStuff
set "originalName=%~1"
set "parentFolder=%~2"
echo move "%originalName%" "%parentFolder:~0,5% %originalName%"
exit /b
The specific bit you're looking for is %parentFolder:~0,5%, which takes a substring of %parentFolder% starting at character 0 and stopping after 5 characters. This gives you the first 5 characters that you're looking for.
The tricky bit is that you can't use this on those for loop %%z type variables. Thus, you have to pass it to another variable. Further, because you've got some nested loops delayed expansion makes this really ugly, so I've passed the variable into a subroutine (call :renameStuff "%%i" "%%~nxz") which turns those into %1 and %2 type variables, and then passed it into ordinary variables (set "originalName=%~1") that this will work with.
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
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!