How to change identical line/dir in multiple bat files? - windows

I would like to update a line in a few different batch files at the same time.
How would I go about writing something to change multiple identical lines in a file.
Specifically, every week I have a BAT file create a new folder labeled "Week monthdayyear"
I have other BATs that have been made to move files to that new folder throughout the week. Currently the only thing I know how to do is manually change the date in each of the BATs that move files to that folder every week. I would like to have my BAT file that creates the new directory to modify the other BATs and input the new directory.
Example:
Each Bat Contains:
ren "...\Week 4-13-2015\..."
I need to change each bat to say
ren "...\Week 4-20-2015\..."
This question is related to another question I asked a week ago. I solved the other question if you want to use it as reference for more background information and the actual scripts.
Can a Batch File Tell a program to save a file as? (If so how)

You have a problem of inter-program communication, that is, the program that create the new folder every week needs to inform to the rest of programs the name of the current folder. You may do that via an auxiliary data file. For example, include this (or similar) line in the program that create the folder:
echo Week %monthdayyear%> CurrentWeek.txt
... and get the right name in each one of the rest of programs:
rem Get the name of the current folder
set /P currentFolder=< CurrentWeek.txt
. . .
ren "...\%currentFolder%\..."

Related

How to copy and rename file in CMD script with a date/version number appended to it

I have a CMD script that currently copies file from one location to another. It overwrites the old file with that name in the destination source which is fine. Now, I would like to keep historic data available in the Archive (separate csvs with different names). I am trying to copy the existing file to the archive before the file is updated with new data. What I thought is to create Archive folder and copy original file to it. However, with the setup I have now, it will be erasing the older version of the file because they would get the same name applied.
I tried adding DAT variable which is a current date and append this in the beginning of the file name but it prompted a syntax error. Not sure if this is even possible in CMD. I would really appreciate some assistance. If you take DAT out of the code, it will work and copy the file fine but the next time I run this script, it will overwrite the file while I want to have different historic files with name containing a date with identifier. If appending date is not possible, perhaps we could create version number by ourselves starting from 1.
Here is the code that I tried:
#ECHO OFF
set day=%date:~0,2%
set month=%date:~3,2%
set year=%date:~6%
SET DAT=%DATE:~6%%DATE:~3,2%
Set ZEIT=%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
copy /Y \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\ZPP00138_TUS350.csv \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\Archive\%DAT%_ZPP00138_TUS350.csv
This line adds an extra blank at the end of the line which causes your trouble.
SET DAT=%DATE:~6%%DATE:~3,2%
copy /Y \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\ZPP00138_TUS350.csv \\srvditz1\DataXchange_R3\TUS_EXCHANGE\TUS300_digi_SFM\30_Visualization\Archive\%DATE:~6%%DATE:~3,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%_ZPP00138_TUS350.csv
If you put directly without any variable this works.

Run batch file located in sub directory with another batch file [duplicate]

This question already has answers here:
Get current batchfile directory
(4 answers)
Closed 1 year ago.
So I know you think I should do something like: start C:\parentfolder\subfolder\batchfile.bat
or something along those but I was wonder if there was like a way to reference the sub directory without using it's actual name? Since the batch files will be generated by another batch file.
Don't ask why I need it to be generated I'd much rather it not but couldn't find any other way.
You have to cd into a subdirectory and then run start start.bat. That should run another batch file. To go into a relative subdirectory, run the command cd foldername. You have to make sure you are currently in the parent directory.

How to extract date from filename in batch, and unzip to multiple directories using batch/7z

I am trying to code a script to automatically process some of our daily ftp files.
I have already coded the files to download from the source ftp using WinSCP and calling it in a .bat file, and would ideally like to call it within the same bat. Scripting Language does not matter, as long as I can run/call it from the original batch.
I need will extract the date from a filename, and unzip the contents into corresponding folders. The source file is delivered automatically daily via FTP, and the filename is:
SOFL_CLAIM_TC201702270720000075.zip
The bolded section is the date that I would like to extract.
The contents of the .zip include two types of content, multiple PDFs and a .dat file.
For the supplied date of 20170227, the pdfs need to get extracted to a folder following the format:
\%root%\FNOIs\2017\02-Feb\02-27-2017
At the same time, the .dat file needs to get extracted to multiple folders following the format:
\%root%\Claim Add\2017 Claim Add\02-2017
\%root2%\vendorFTP\VendorFolder
After extracting, I need to move the source zip to
\%root%\Claim Add\2017 Claim Add\02-2017
What is the best way off accomplishing all of this?
I am assuming it would be the for /f batch command, but I am new to batch coding and cannot figure out how to start it from scratch.
I also have 7zip installed, but do not understand how to use the command-line options.
You have asked for a lot in one question, and not shown any code or demonstrated effort on your part.
For the first part, once you have the filename in a variable:
set FILENAME=SOFL_CLAIM_TC201702270720000075.zip
You can get the date part with:
echo %FILENAME:~13,-14%
The syntax: :13,-14 means "Remove the first 13 letters and the last 14 letters." That should leave you with just the date.
When you integrate that into your script, Show Your Code

Get location (path) of the shorctut calling a batch file

I have an ongoing list of 'Enquiry' folders within a directory. I'm trying to create a batch file that can sit within each of these folders that, when run, will move the folder it resides in to a 'Projects' folder.
From my attempts, and as half expected, I'm realising it's not possible to move the folder containing the batch file (or copy folder and then delete source folder containing the batch), so my second thought is to position a single 'master' batch file outside of the 'Enquiry' folders, and have a shortcut within each Enquiry folder that links to that 'external' batch file.
For this, I'd need to obtain the location path of the shortcut calling the batch, is this possible?
Or is there a better way to achieve my aim?
Just an extra note in case it helps anyone, I had to make the following changes to ensure file path with space was ok (putting %enqfolder% in quotes) and I also need to a '\' after 'projects' in the move command. Thanks again to #Klitos.
setlocal
set enqfolder=%cd%
cd ..
move "%enqfolder%" ..\projects\
Following on from the comments to your question, the reason it doesn't move the folder is that when the batch file runs, the current directory is still the enquiry folder. You need to move out of it and then move it. Also, as it turns out, you don't have to delegate to a "master" batch file either. You can just do the move in the same batch file (but the move should be the last line of the batch file; once the batch file itself moves, the command processor won't be able to read the following lines).
setlocal
set enqfolder=%cd%
cd ..
move %enqfolder% ..\projects

How do I find current directory for my Batch file?

I am currently working on a batch file (let's call it temp.bat). I am trying to move the batch file to my desktop, but I can't find my batch file location / directory. I am asking if there is an extension or something that can use to automatically identify the directory, instead of entering it every time. In other words, I can still move the file to another place even in another directory. Example:
temp.bat is currently on my Downloads file. I use the move command to move the file to Desktop. Now temp.bat is currently on my Documents file. I try to use the same command to move the file to Desktop.
Is there any parameter extensions for it?
This is what I have now:
#echo off
move /y [The directory of temp.bat] C:\Users\69Swag\Desktop
All answers appreciated! Thanks :)
You don't need the current directory to move a file.
move temp.bat C:\Users\69Swag\Desktop
If you still want the current directory, use this:
echo %cd%

Resources