Programming a "Not - Gate" situation into a batch file? - windows

Brand new to coding of any form. Simply put: I have 2 batch files written. 1 loads one configuration into a program (MultiMonitor), the other loads a 2nd configuration. Id like a 3rd file to act as a "light switch" Or a "NOT-Gate." Effectively, if last time it was ran, it launched the bat controlling config 1, this time it should load config 2.
Currently I just launch the appropriate .bat; but id like to simplify this so I only have one file to launch and then it chooses the one not chosen last time.

This is easy:
In option1.bat (in addition to the real work):
echo call option2 > autoselect.bat
In option2.bat
echo call option1 > autoselect.bat

There are a few ways you could do this:
Use a permanent environmental variable using setx - NOT RECOMMENDED
Test which config is currently being used by your program - I am unfamiliar with your program so I do not know if this is even possible.
Use a temporary file
This example shows how to do this with a temp file:
if not exist file.ext (
rem load config #1
echo.>file.ext
) else (
rem load config #2
del file.ext
)
The file can be named whatever you want.

Related

Windows 10 command line/script - for or loop filename(1), filename(2)

I am writing a script (batch file) currently to run at Windows Command Line. The task is I have a variable number of files in a folder. The files are named in the format of filename(1), filename(2) ... etc.
I am trying to set a FOR loop or using a IF ... GOTO to move filenames(1 to 9) to one folder, then filenames (10 to 99) to another folder and so on so I can deal with them separately.
So, easy way would be to copy in sequence to join to source files into a single target file. Alternatively a way to construct a filename for a copy and/or move command that looked at files such as test(1), test(2), ... test(9) in order and then stop.
Files are sorted in folder in following order - test(1).txt test(2).txt test(3).txt test(4).txt test(5).txt test(6).txt test(7).txt test(8).txt test(9).txt
There may only be two or three files however, or there maybe 1000 files in the folder to be dealt with.
I have been trying to set something like this up:
set count1=9
set count2=99
set count_loop=1
:while_loop1
echo "count_loop = " %count_loop%
move test(%count_loop%) proto\9\
set /a "count_loop = count_loop + 1"
if %count_loop% leq %count1% (
echo test(%count_loop%) moved
goto while_loop1)
I could do with some advice here.
Cheers, Thomo the Lost

Windows batch for loop sub routines and local variables

At work we use Windows batch files to run jobs off a scheduling tool. It's an old tool and approach but it works. I have to make new jobs fit the existing technology.
For this particular job I need pick up a number of files from a folder and load them into a SQL server database table. For each one I load I then need to run a bit of SQL to update the recently populated table.
So I have a for loop that looks for files in the folder. If one exists I use SQL server's BCP to load the data into a table. I then use SQL server's SQLCMD to perform the update.
The policy is when using BCP or SQLCMD to write any issues out to files. Post call we then check these files to ensure that nothing went wrong.
When dealing with a single file this is fairly straightforward stuff. Using a FOR loop is proving to be impossible.
I envisaged calling a sub routine which would set a variable that code in the for loop could check and if appropriate exit gracefully.
The first thing we do in the batch file is set a global variable to 0. When the batch job returns control to the 'scheduler' if this is not equal to 0 the job shows as failed.
So paraphrasing the code it looks something like the following.
SET g_status=0
SET LOCAL EnableDelayedExpansion
FOR n = 1 to 12
IF FILE(n) EXISTS (
Execute BCP....
Execute SQLCMD....
CALL check_sql_status
IF !l_error_status! NEQ 0 ENDLOCAL & SET g_status=!l_error_status!
IF g_status NEQ 0 GOTO exit_gracefully
)
NEXT
.
.
GOTO END
REM ========
:check_sql_status
REM======
IF something is wrong (
SET l_error_status=123
GOTO EOF
REM ========
:exit_gracefully
REM======
ECHO g_status
:End
.
.
Don't get hung up on the 'code' above. It's only the bit where I am attempting pass the local variable back to the global variable that I am interested in. No matter what I try I cannot get the global variable to accept the value set in the sub routine.
I will try to find this question and post some of the actual code when I get back to work although BCP and SQLCMD use lots of parameters that may just confuse rather than help.

How to change the output file name in a bat file?

I'm trying to change the output file name, fOut, in a bat file, but have no luck so far.
I'm developing on Windows 7 and will deploy the code to Windows 2003 server.
The code looks like this:
set fName=%1
set fExt=%fName:~-5,-1%
set fOut=%fName:~0,-5%_PAD%fName:~-5%
Examples of fOut:
abcdc2evv_PAD.dat
abcdefgh33ij_3737_PAD.dat
How can I change fOut to get the following file names?
A. Adding FMT_ at the beginning of the file name:
FMT_abcdc2evv_PAD.dat
FMT_abcdefgh33ij_3737_PAD.dat
B. Adding FMT_ at the beginning of the file name and remove _PAD before .dat:
FMT_abcdc2evv.dat
FMT_abcdefgh33ij_3737.dat
Addendum:
Just one argument is passed to the bat file: path + file name.
x.bat "C\test\xxx.dat"
In the bat file:
#echo ^-input file name = ^%1
set fName=%1
set fExt=%fName:~-5,-1%
set fOut==%fName:~0,-5%_PAD%fName:~-5%
I don't know if I'm missing something obvious - it's not clear what the input to this script is.
However adding FMT_ before should just be a case of changing:
set fOut=%fName:~0,-5%_PAD%fName:~-5%
to:
set fOut=FMT_%fName:~0,-5%_PAD%fName:~-5%
or if you want to put the FMT_ version into another variable, then:
set bob=FMT_%fOut%
As for removing _PAD, can you not just repeat the SET fOut line without the _PAD? This would seem to be the simplest way to do it. In fact, removing _PAD and prefixing FMT_ would seem to simply be this:
set bob=FMT_%1
if you want to remove pad just take it out of your assignment statement
you have:
set fOut=%fName:~0,-5%_PAD%fName:~-5%
you want:
set fOut=%fName:~0,-5%fName:~-5%
to add FMT_ just add it at the beginning of the file name:
set fOut=%FMT_%fName:~0,-5%_PAD%fName:~-5%
If you want to separate the filename from the extension, don't mess around counting chars; there is a built-in method (described in for /?):
echo Filename=%~n1
echo Extension=%~x1
echo resulting file="FMT_%~1"
REM without _PAD, following with _PAD
set filename="FMT_%~n1_PAD%~x1"
If there is really need to remove _PAD (as Chris already noted, you are explicitely adding it with your code), just replace _PAD. with . only:
set filename=%filename:_PAD.=.%

CMD to pick up specific files using a wild card

I have a CMD script I have been working on that reads a folder that has 3 different files in it. They have names like the following:
File1_ddmmyyyy.txt
File2_ddmmyyyy.txt
file3_ddmmyyyy.txt
I can use * to identify the files but the next step that uses the file name (Launching an ETL program) requires that the full file path. It is unable to identify the file (is entered as: file3_*.txt Literally)
Does anyone have and ideas on how I can grab this file based on the Wildcard, then obtain the full name to use later?
Thanks
EDIT - Example-
There is one ETL File executable line that matches up with each of the files based on the file name. for example:
File1_ddmmyyyy.txt is matched with ETL file File1.tf.map (Based on the file name and ETL file name) it is a one to one match (3 files to load and 3 ETL files to load them)
DJENGINE -sc "Y:\FileLocation\%Year_Mo_Da%\File1*.txt"-tc Server="X";Database="Y";Table="dbo.File1" "Y:\MapLocation\File1.tf.xml" (FileName = File1_ddmmyyyy.txt)
DJENGINE -sc "Y:\FileLocation\%Year_Mo_Da%\File2*.txt"-tc Server="X";Database="Y";Table="dbo.File2" "Y:\MapLocation\File2.tf.xml" (FileName = File2_ddmmyyyy.txt)
DJENGINE -sc "Y:\FileLocation\%Year_Mo_Da%\File3*.txt"-tc Server="X";Database="Y";Table="dbo.File3" "Y:\MapLocation\File3.tf.xml" (FileName = File3_ddmmyyyy.txt)
In the executable line of code for the ETL program instead of referencing the actual file name (Needed to run) it is trying to run File1*.txt Not filling in the variable.
My only thought would be since I have only 3 files and three ETL files that match base on the root file names I can place each of the files full names in a variable prior to this then use each of them in the executable ETL lines.
Not sure if this would work or how to do it. Let me know if this helps.
Hmm - started off clear as mud.
Then Taz arrived with his mixmaster on Turbo...
What congeals from this appears to be:
There's a directory "Y:\FileLocation\yyyy_mm_dd which contains 3 text files FileNddmmyyyy.txt
where dd is day number, mm month number, yyyy 4-digit year number and N will be 1..3.
The requirement from there is to build an appropriate command using the template
DJENGINE -sc "Y:\FileLocation\yyyy_mm_dd\FileN_ddmmyyyy.txt" -tc Server="X";Database="Y";Table="dbo.FileN" "Y:\MapLocation\FileN.tf.xml"
where dd,mm,yyyy,N have the same meanings, for N=1..3
#ECHO OFF
SETLOCAL
SET "yyyymmdd=%1"
IF NOT DEFINED yyyymmdd for /f "skip=1 delims=" %%x in ('wmic os get localdatetime') do set yyyymmdd=%%x&GOTO gotdate
:gotdate
SET "yyyymmdd=%yyyymmdd:~0,8%"&SET "yyyy=%yyyymmdd:~0,4%"&SET "mm=%yyyymmdd:~4,2%"&SET "dd=%yyyymmdd:~6,2%"
FOR %%a IN (1,2,3) DO ECHO(DJENGINE -sc "Y:\FileLocation\%yyyy%_%mm%_%dd%\File%%a_%dd%%mm%%yyyy%.txt" -tc Server="X";Database="Y";Table="dbo.File%%a" "Y:\MapLocation\File%%a.tf.xml"
GOTO :EOF
In the absence of any information about where yyyymmdd come from, the above should fill in the values for today.
If the procedure is provided with a parameter in the format yyyymmdd like this:
thisbatch 20140726
then the procedure will generate lines for July 26th 2014 (note that the value provided here is not checked for validity)
The required DJENGINE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DJENGINE to DJENGINE to actually execute the program on the files. This presumes that DJENGINE is an executable, not a batch procedure.

Windows Batch file Dynamic create statements

I need to get a list of file names from a directory using a windows batch program. I would like to take each FILE NAME and combine that with another command line statement.
Note i only need the file name not the contents of the file.
How would this be done?
If i have a 'Data' directory on the D drive with the below files (note there could be many files)
--------------
myFile1.abc
myfile2.abc
------------------
How could i dynamically create something like this using a windows batch program?
move C:\myFile1.abc C:\newdir
move C:\myFile2.abc C:\newdir
note - (i know there is a easier way move files but but i am trying to understand the logic so i can use it in a different command)
You can use a for loop:
for %%X in (D:\*) do (
echo move %%X C:\newdir
)
Try on the command line:
for %X in (D:\DataFiles\*) do echo move "%~fX" C:\newdir>>logfile.txt
It puts all file names from D:\DataFiles in logfile.txt (except hidden files).

Resources