At the following location
\ncsusnasent02.na.jnj.com\its_diq_na_win_dev\PowerCenter\infa_shared\WCPIT_BIO_EDW\SrcFiles\DDDMD\DDD.CLI026.WK0933.DDDMR45.001.head
I have one file
DDD.CLI026.WK0933.DDDMR45.001.head
if i open this file
i get data as following(in a single line)
HEADER0101IMS HEALTHDMD Weekly D DD.CLI026.WK0933.DDDMR45 Centocor DMDDRM45 W2009080210120090831125325ssnyder#us.imshealth.com
TRAIL0101 000000000581 0000000000CKSUM000002236804730
we need to copy 581(it will not be same always it gets updated everyday) from this file
and put it in a variable
you can try the below. It will set the field into the environment variable id:
for /f "tokens=10" %%a IN (%1) do (
SET id=%%a
)
echo %id%
You can pass the full path and file name into the bat as the first argument.
edit:
This simple bat will take the input from the file you specify on the commandline (param %1), it will use the default separators of <space> and <tab> to break the line in your file - defined in the IN set - into a set of tokens. The "tokens=10" param tells the processor to pass the 10th token, which turns out to be your number in question, into the DO block. It is passed in as a param %%a. Within the DO block, I simply assign that value to an environment variable id. After the for command is complete, I echo the value out to the console.
Take a look at the FOR command, specifically the part about the /F parameter.
I'm not certain enough about the structure of that line to even try to write the full command, but you should be able to write it yourself given that information.
Hmm to me it looks more like the guy needs a dos substr... i.e.
#Echo Off
If not %1.==[]. (Cmd /V:On /C Call %0 [] %1 & GoTo :EOF)
Shift
Set MyVariable=HELLOWORLD
Set ASubStr=!MyVariable:~%1!
Echo [!ASubStr!]
So for example save this as test.bat and then call "test.bat 5" and it will echo WORLD
Google DOS Substring and work out how to parse your text variable the way you want it.
Related
This script looks at a network location for a folder name and specific file and then should copy the file to other folder on the network with current timestamp in destination file name.
Can you advise any syntax error or reason why it is not copying?
ECHO on
Title %0
set GETfn=Q:\Cdata\mm_tn
set GETfn=%GETfn: =%
echo GETfn = %GETfn%
set f1=%GETfn%%m%%d%%y%-%hr%%mn%.csv
set f1=%f1: =%
echo f1 = %f1%
copy GETfn.csv Q:\FTP\Sent\%f1%
dir Q:\FTP\Sent\
For example, if i have specificfile.csv on a mapped network drive Q:\Cdata\mm_tn\
then using this:
#echo off
:: src -> destination
set src=Q:\Cdata\mm_tn\
set dst=Q:\FTP\Sent\
echo -------------------------------------
echo source dir ---^> %src%
echo destination dir ---^> %dst%
echo -------------------------------------
:: timestamp
:: https://stackoverflow.com/questions/1064557/creating-a-file-name-as-a-timestamp-in-a-batch-job
FOR /F %%A IN ('WMIC OS GET LocalDateTime ^| FINDSTR \.') DO (
#SET B=%%A
)
set timestamp=%B:~4,2%_%B:~6,2%_%B:~0,4%_%B:~8,2%-%B:~10,2%
echo %timestamp%
:: copy from Q:\Cdata\mm_tn\ -> Q:\FTP\Sent\
copy %src%specificfile.csv %dst%%timestamp%_specificfile.csv
the specificfile.csv is copied to Q:\FTP\Sent\ as timestamped file 12_17_2020_12-39_specificfile.csv. Now paths can be easily adjusted for your requirements.
The second block is:
set GETfn=Q:\Cdata\mm_tn
set GETfn=%GETfn: =%
echo GETfn = %GETfn%
This block first defines the environment variable GETfn with string value Q:\Cdata\mm_tn. Next it uses a string substitution to remove all spaces from the string assigned to environment variable GETfn. That is of course a completely useless command line as the folder path assigned to the environment variable does not contain any space at all on having the batch file written without trailing space(s) on the line above. The last line of this block just outputs the fixed folder path which makes also no real sense.
The third block is:
set f1=%GETfn%%m%%d%%y%-%hr%%mn%.csv
set f1=%f1: =%
echo f1 = %f1%
It defines an environment variable f1 being a concatenation of the strings assigned to the environment variables GETfn (defined above), m, d, y, hr and mn not defined in posted code at all. So f1 is defined with Q:\Cdata\mm_tn-.csv which is of course not right. The next line is again useless as it removes all spaces from the string assigned to environment variable f1 not containing any space at all, except the batch file contains one or more trailing spaces at end of the line above. The third line just outputs the wrong defined environment variable f1.
I recommend to open a command prompt, run set /? and read the output help carefully and completely from top of first to bottom of last page. Next I suggest to read my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It has some useful additional information about the usage of command SET.
Please read carefully and completely my answer on Time is set incorrectly after midnight. Then you should have the knowledge why the following command line is a replacement for the entire posted batch file code.
#if exist "Q:\Cdata\mm_tn\GETfn.csv" for /F "tokens=1-5 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do #copy "Q:\Cdata\mm_tn\GETfn.csv" "Q:\FTP\Sent\GETfn_%%J%%K%%I-%%L%%M.csv" & goto :EOF
That command line copies the file Q:\Cdata\mm_tn\GETfn.csv to directory Q:\FTP\Sent with new file name GETfn_MMddyyyy-hhmm.csv using current date/time.
I would not use this date format for the destination file name. It would be better to use GETfn_yyyyMMdd-hhmm.csv, or better to read GETfn_yyyy-MM-dd_hh-mm.csv. This is the international date format which has the big advantage that files sorted by name are with such a date/time format sorted at the same time in chronological order. That is very often very helpful. So better in my opinion would be:
#if exist "Q:\Cdata\mm_tn\GETfn.csv" for /F "tokens=1-5 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do #copy "Q:\Cdata\mm_tn\GETfn.csv" "Q:\FTP\Sent\GETfn_%%I-%%J-%%K_%%L_%%M.csv" & goto :EOF
For understanding the used commands in the single command line and how they work, open a window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
for /?
goto /?
if /?
robocopy /?
See also:
Where does GOTO :EOF return to?
Single line with multiple commands using Windows batch file
I am making a query on a txt file via Windows command prompt, but the output displayed is actual content of txt file, instead of system variable.
Any idea how can I amend that?
FOR /F "tokens=1,2 delims=," %%G IN (Backup_Folder.txt) DO (
echo %%H
)
Output is
%USERPROFILE%\Documents\Electronic Arts
instead of
C:\Users\UserName\Documents\Electronic Arts
Content of Backup_Folder.txt is:
EA,%USERPROFILE%\Documents\Electronic Arts
I suggest to use in the batch file:
for /F "tokens=1* delims=," %%G IN (Backup_Folder.txt) do call echo %%H
It is better to use tokens=1* instead of tokens=1,2 in case of the folder path stored in the text file contains per chance also a comma, for example:
EA,%USERPROFILE%\Documents,Settings\Electronic Arts
The asterisk after 1 means the rest of the line after the comma(s) after first comma delimited string without further splitting up the line on commas. So with tokens=1* the loop variable G gets assigned EA and loop variable H gets assigned %USERPROFILE%\Documents,Settings\Electronic Arts. The usage of tokens=1,2 results in H gets assigned just %USERPROFILE%\Documents, for that example.
The command CALL is necessary in this specific case to get on execution of the body of the loop after replacing %%H by %USERPROFILE%\Documents\Electronic Arts in first parsing step the command line parsed a second time by Windows command interpreter to expand additionally %USERPROFILE%.
When the folder path read from text file is assigned to an environment variable, I recommend to use as command set "FolderPath=%%~H" or call set "FolderPath=%%~H" for getting all environment variable references in folder path already expanded before assigning the folder path to the environment variable.
The tilde character between %% and H results on a folder path like "%USERPROFILE%\Documents & Settings\Electronic Arts" stored in the text file that the surrounding double quotes are removed making it possible to use the environment variable for example in a command line like:
if exist "%FolderPath%\Game\game.exe" echo Game exists.
A space as well as &()[]{}^=;!'+,`~ in a file/folder name without or with path require the usage of surrounding double quotes as output by Windows command interpreter on last help page on running in a command prompt window cmd /?. In argument strings not being a file/folder name/path the additionally possible characters |<> require also that the entire argument string is enclosed in straight double quotes to get those redirection operators interpreted as literal characters.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
for /?
set /?
I am creating a generic script that will parse a statement and return the result. I have done so in shell, but don't know how this can happen in batch scripting
Script 1 Main script (main.bat).
SET Myresult=CALL child.bat "Statement"
Now Myresult should store the answer whatever I want to return in this variable.
Solution 1 : SET Myresult in Child.bat and use it in main.bat but now what if the user does not know what the variable name is .
So is their a way to return a value like in java
return xyz
xyz gets captured in the call statement elsewhere.
-------------------------------xxxxxxxxxxxxxxxxxxxxxxx-----------
PART 2
Here are the details of what I am doing .
The so called child script is getSQLResult.bat
What is does is
getSQLResult.bat -q "Select a from abc"
Now this above call statement can be used by anyone any how in any batch script .
So apart from passing a variablename(return name) or writing a for loop to parse the result set is there any simple straight forward way .
Option 1 - Pass the variable name
main.bat
call child.bat myresult "Statement"
echo %myresult%
child.bat
set "%~1=argument was %~2"
Option 2 - Process output of child process
main.bat
for /f "delims=" %%a in ('child.bat "Statement"') do set "myresult=%%a"
echo %myresult%
child.bat
#echo off
echo Argument was %~1
or you can use temporary files, the registry, the clipboard, ... to pass the information, but in any case if you are coding a element (your child.bat) to be reused by you or another person, you are creating an interface between this element and the rest of the code, a expected set of input arguments and a way to return information. Your question
... what if the user does not know what the variable name is?
is answered by the documentation of this interface.
This is the way to do it in batch:
for /f "delims=" %%a in ('echo hello world') do set myresult=%%a
echo it returned: %myresult%
(echo hello world is just an example)
Note: this will return the last (or only) line only. It can be modified to process more lines, but your example implies, there is only one line of output)
EDIT If I understand your edit correct, you want to pass just a statement to an external batchfile, which does the work and gives back the result into a variable, which is defined with the call.
REM child.bat
#echo off
set var=%1
for /f "tokens=1,*" %%i in ("%*") do (
set var=%%i
set statement="%%~j"
)
for /f "delims=" %%a in ('getSQLResult.bat -q %statement%') do set "%var%=%%a"
use it with:
call child.bat result "Select a from abc"
echo %result%
of course it will still return the last (or only) line. Can be easily modified to get the first line. If the output could be more lines, I suggest using an "array" with an counter to avoid the problems, MC ND adresses in his comment to your question. Maybe returning a filename to a file with the multiline output could be another solution.
I'm over my head with this - spent too much time searching already - evidently I don't understand the basics of CMD variables etc. - and it always gives me such a headache
why wouldn't this work?
for %a in (*) do ( set tmpx=%a & echo %tmpx% )
the above code outputs the value of %tmpx% in some other scope - and it is always constant
yes, i run setlocal ENABLEDELAYEDEXPANSION
basically i need to do a simple rename of all files in folder from constantstring_somenameXX.tif to somenameXX.tif, where i.e. constantstring=0000000005
i had to use set because other posts rightly suggested that %a in a for loop has a special behaviour, and the substitutions wouldn't work for it as it is.
i would prefer not to use scripts and/or powershell - unless not using them is impossible
thank you
for %a in (*) do ( set tmpx=%a & echo %tmpx% )
The problem with the previous code is delayed expansion. Yes, you enabled it, but you have not used it, and depending on how you enabled it, it will not work
In cmd, when a line or block of lines (code inside parenthesis) is reached, it is first parsed and then executed. During the parse phase, variable read operations are removed from the command, replaced with the value in the variable before the command starts to execute. So, if you change the value of a variable inside a line/block you can not retrieve the changed value inside the same line/block as there are no variable reads (they were replaced)
setlocal enabledelayedexpansion allows to replace (where needed) the variable read syntax from %var% to !var!, indicating to the parser that the read operation will be delayed until the execution phase.
So, in your case, your code should have been something like
setlocal enabledelayedexpansion & for %a in (*) do ( set "tmpx=%a" & echo !tmpx! )
BUT this will not work (in default configured environments).
cmd has two execution modes: batch file and command line. In your case, you are using command line (no escaped percent sign in for loop) and in command line mode the setlocal enabledelayedexpansion will not work. It is intended for batch files (see setlocal /?)
How to make it work from the command line? By default cmd is started with delayed expansion disabled and you can not enable it if not inside a batch file. But you can start cmd with delayed expansion enabled and run your command in this started instance (see cmd /?)
cmd /v:on /c "for %a in (*) do ( set "tmpx=%a" & echo !tmpx! )"
Anyway, to solve your rename problem, delayed expansion is not needed
for %a in (*_*.tif) do for /f "tokens=1,* delims=_" %b in ("%~nxa") do echo ren "%a" "%c"
That is, for each tif file with an underscore, take the name and extension of the file (%~nxa) as a string, and using the underscore as a delimiter between tokens, retrieve the first token (the text on the left of the first underscore) in %b and the rest of the text (to the right of the underscore) into %c. Now, just rename the original file name (stored in %a) to the contents of %c (the text on the right of the underscore)
In this code rename operations are only echoed to console. If the output is correct, remove the echo command.
! is the character to use rather than % when wanting execution time value. % does when it's read value.
CMD was written by IBM engineers and they were trying to make MSDos a programming language while making sure Dos commands ran the same. So we get a hodge podge.
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed to a program
" parameters with spaces must be enclosed in quotes
+ used with copy to concatinate files. E.G. copy file1+file2 newfile
, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,
%variablename% a inbuilt or user set environmental variable
!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command
%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.
%* (%*) the entire command line.
%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.
I am running an executable in a batch file with two parameters;
cmd /k ""executable" "param1" "param2""
This returns a string that I want to launch. I can't figure out how to set this return in a variable and subsequently launch it in IE.
Any ideas?
If the returned string contains a single line you may use FOR /F to set the value of an environment variable. For example:
s1.cmd
echo this is a one line string
s2.cmd
#SETLOCAL
#ECHO OFF
for /f "tokens=*" %%a in ('cmd /c s1.cmd') do set MY_VAR=%%a
echo got: %MY_VAR%
ENDLOCAL
Result
C:\> s2.cmd
got: this is a one line string
C:\>
You can use the following syntax to capture the output of your executable into a variable:
FOR /F "tokens=*" %%i in ('%~dp0YOUR_APP.exe') do SET TOOLOUTPUT=%%i
Source
then you can pass the value on to IE like so:
START "YOUR_WINDOW_NAME" /MAX /D"C:\Program Files\Internet Explorer\" iexplore %TOOLOUTPUT%
I take it that the application code that determines the url is too complicated to be reproduced in a batch file directly, or the source to the executable has been lost. If not I personally would prefer to have the logic visible in the batch file itself.
start %1 %2
Edit: Romulo A. Ceccon posted a much better solution which doesn't involve any file system access and dirty tricks. Left this here for reference (it works with command.com as well if you need 9x compatibility), but please prefer Romulo's solution.
Go through an environment variable you set by using an intermediate helper script you dynamically generate from a template. You will need write permissions somewhere, otherwise it cannot be done (the Windows command shell language is very, very limited.)
Let's call your helper script template helper.tpl with the following contents:
set INTERMEDVAR=
Make sure that helper.tpl has only a single line (no trailing CRLF!) and make sure you don't have any spaces after the equals sign there.
Now, in your main script, capture the output from your command into a temporary file (let's call it my_output_file.tmp):
cmd /k ""executable" "param1" "param2"" > my_output_file.tmp
Then copy the contents of the helper template and the output together into your helper script, let's call it my_helper_script.cmd:
copy /b helper.tpl + my_output_file.tmp my_helper_script.cmd
Then evaluate the helper script in the current context:
call my_helper_script.cmd
Now the INTERMEDVAR variable is set to the first line of the output from "executable" (if it outputs more than one line, you're on your own...) You can now invoke IE:
start iexplore.exe "%INTERMEDVAR%"
And don't forget to clean up the created files:
del /q /f my_output_file.tmp my_helper_script.cmd
This will obviously not work when invoked multiple times in parallel - you'll have to parametrize the temporary file and helper script names using the current cmd.exe's PID (for example) so that they won't overwrite each other's output, but the principle is the same.
However, if you can get a real shell, use that. cmd.exe is extremely cumbersome.