I am trying to copy a specific line out of a file to a variable in a batch file.
Basically I need to copy line 6 of a file and make it into a variable.
Any help is appreciated.
for /f "skip=5delims=" %%i in (filename) do set "var=%%i"&goto nextline
:nextline
Should do the job.
Related
I have a windows batch file that reads in various lines of a text file for processing. During the execution I have to open another text file that has only one line in it. The line contains a serial number with about 3 or 4 spaces after the end.
I need to read in this file called (value.txt), grab the data in the first line and assign the value to a variable without any spaces at the end.
After that I need to add the .pdf extension to this variable and set to another variable.
I then need to then check if the variable with the pdf extension exists in the current directory as a file name. If it does then can I change the variable value to add -1 before the .pdf. If this file still exists then try -2 etc.
Thus
File name read in called value.txt
55400TERM1 (variable VAR1 is assigned with no spaces)
55400TERM1.pdf (variable VAR2 is set with the .pdf extension)
55400TERM1-1.pdf (variable VAR2 is updated if 55400TERM.pdf exists)
55400TERM1-2.pdf (variable VAR2 is updated if 55400TERM1.pdf exists)
Etc etc - loops until it cannot file a existing file with the variable value.
Here is a comment batch script for this task:
#echo off
rem If text file with file name does not exist in current
rem directory, exit this batch file as nothing can be done.
if not exist value.txt (
echo %~f0: Missing file "value.txt" in "%CD%".
goto :EOF
)
rem Read first line from text file. Command FOR automatically splits up the
rem line into strings using space and horizontal tab character as string
rem delimiter. As the line contains just one string with trailing spaces
rem most likely caused by wrong redirecting an output into the text file
rem the single string is assigned to the variable with no trailing spaces.
for /F %%I in (value.txt) do (
set "FileName=%%I"
goto CheckFile
)
echo %~f0: No string in file "value.txt" in "%CD%".
goto :EOF
:CheckFile
if exist "%FileName%.pdf" goto FindLatestFile
echo No file "%FileName%.pdf" found in "%CD%".
set "FileName=%FileName%.pdf"
goto ProcessFile
rem The command DIR as used here lists all files matching the wildcard
rem specification in reverse order according to last modification date.
rem This batch file assumes that the file with highest number is the
rem newest modified file. This speeds up determining the file with the
rem highest number in file name. For an alphabetical reverse order it
rem would be necessary that all files have the same number of digits,
rem i.e. the files are *-01.pdf, *-02.pdf, ..., *-10.pdf, *-11.pdf as
rem otherwise the alphabetical order would be *-1.pdf, *-10.pdf, *-11.pdf,
rem *-2.pdf, ..., *-9.pdf and then last file would be determined wrong.
:FindLatestFile
for /F %%I in ('dir /O-D /TW /B "%FileName%-*.pdf" 2^>nul') do (
set "LastFileName=%%~nI"
goto FoundLatestFile
)
echo No file "%FileName%-*.pdf" found in "%CD%".
set "FileName=%FileName%-1.pdf"
goto ProcessFile
:FoundLatestFile
for /F "tokens=2 delims=-" %%I in ("%LastFileName%") do set "LastFileNumber=%%I"
set /A LastFileNumber+=1
set "FileName=%FileName%-%LastFileNumber%.pdf"
:ProcessFile
echo Next file is: %FileName%
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
dir /?
for /?
goto /?
if /?
rem /?
set /?
I am trying to launch a specific Excel document from the command line and pass in a variable in the form of a file path. On starting the Excel sheet runs a macro. This file path may contain spaces however when Excel interprets this it, I believe it tries to open up each section after a space as a new workbook. This results in a number of error warnings after the program runs as it obviously cannot file created from the substring.
The batch file looks like this
echo %~1
start excel.exe %USERPROFILE%\Desktop\Compare.xlsm /e/%1%
pause
EDIT: For Clarification. The batch file is activate when a file is drag and dropped onto it. The path of the file dropped is then stored in the %1 variable. If this file path has spaces then after each space Excel assumes that this is a new worksheet and tries to open it. The call to the Compare.xlsm which contains a macro that is going to use the path of the dragged file works correctly as it will always be on the desktop. My issue therefore is how to get Excel to take the entire path name stored in %1 and use it as one command line parameter rather than several calls to open new workbooks.
If the file that is used on the batch file does not contain any spaces then the errors do not occur. Is there any way of getting rid of the errors when using a file path that might have spaces e.g. C:\Users\My Documents\foobar.txt
Try this (TRIED AND TESTED)
echo %~1
start "excel.exe" "%USERPROFILE%\Desktop\Compare.xlsm" /e/%1%
pause
Notice the quotes around "Excel.Exe" and the file path?
Another example
echo %~1
start "excel.exe" "%USERPROFILE%\Desktop\Blah Blah.xlsm" /e/%1%
pause
I am assuming that you are running the code from a .Bat file
as Sid says, any paths that contain spaces must be quoted.
so to use a pth such as C:\My Documents\fubar.txt you would have this:
"C:\My Documents\fubar.txt"
and in your example:
echo
%~1
start excel.exe "%USERPROFILE%\Desktop\Compare.xlsm" /e/%1%
pause
EDIT:
When using a variable as the path, you need to include the quotes in the variable!
I used an excel template. In my ssis package at first the template is copied in the working directory and package execute. In the next run the excel file which is already existed in the working directory it moved into BACKUP folder and again the template file copied. It works good.
But i want to do something when i move it i want to rename it like is the previous file name is Input_01 in the next move it will input_02. How can i do this?
i am using Execute process task toolbox in SSIS pacakge.
and a .bat file is called. in the .bat file i write
move "E:\InputFolder\Input.xls" "D:\Backup"
copy "E:\Template\Input.xls" "E:\InputFolder\Input.xls"
I want a ouput in the backup folder the excel files are like
Input_01,Input_02.... what will be my command?
Here's how to do it with a batch file:
#echo off
set cnt=1
for /f %%f in ('dir /b "D:\Backup\Input_*.xls"') do set /a cnt+=1
if %cnt% lss 10 (move "E:\InputFolder\Input.xls" "D:\Backup\Input_0%cnt%.xls") else (move "E:\InputFolder\Input.xls" "D:\Backup\Input_%cnt%.xls")
copy "E:\Template\Input.xls" "E:\InputFolder\Input.xls"
You can have more control over your files' naming convention (not to mention deletion of old files) if you use a Script Task, though.
can we use a 'File system task' instead. It helps us Renaming a file. You will have to use a 'For-Each loop Container' as well.
I have a large amount of files to Rename based on User input variable and then copy to a specific location. Below is a sample of what I have thus far. It appears as though the Set /p is recognized in the Rename command by not when it goes to Copy it to the new dir. Any help would be appreciated.
#Echo OFF
Echo "Blahhh Rename and Copy. Press Enter."
Pause
SET /p SN=EnterServerName-
Echo You Entered - %SN%
Pause
Rename C:\ctemp\WorkingCSRs\key.pem %SN%_Key.bin
Copy C:\ctemp\WorkingCSRs\Certificates\%SN%_Key.bin C:\ctemp\WorkingCSRs\Certificates\folder /y
You're renaming in C:\ctemp\WorkingCSRs, but the renamed file is expected to be in C:\ctemp\WorkingCSRs\Certificates when copying.
Also you could do the copy as (assuming you don't need the file to be renamed):
Copy C:\ctemp\WorkingCSRs\key.pem C:\ctemp\WorkingCSRs\Certificates\folder\%SN%_Key.bin /y
i want to know how to run this small line of code without using batch file on Excel VBA
#echo off >summary.txt (
for %%F in (*chkpackage*) do findstr /l %1 "%%F" nul||echo %%F:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A:N/A
)
i just want to use only vba or only run cmd line to run this line of code
thx you :)
Change this line from your other question:
If fle.Name Like "*chkpackage.log" Then
To:
If fle.Name Like "*chkpackage*" Then