Windows command line script has embedded blank character [duplicate] - variables

This question already has answers here:
What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean?
(2 answers)
Closed 4 years ago.
I want to copy a file to another file that contains the time and date
in its name.
I use the statement below but the problem is for time values earlier than 10 AM (for which the hour value is only a single digit) there is a blank character instead of a leading zero, which I want.
copy "M:\Production Schedule.xlsm" m:\gsdBackups\ProductionSchedule%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,0%.xlsm
In the example above when I run it at 9:50 I get the resulting name:
GSDProductionSchedule20180509_ 950.xlsm
I do not understand all of the formatting that is going on in the above copy command. Rather than the " 950" below I'd like to have "0950"

As a recurring task this should be a batch file hiding the details.
use wmic or PowerShell to get date/time in a user settings /locale independent format
name the batch file to any name with the extension .bat or .cmd and place it in a folder which is in the path
:: Q:\Test\2018\05\09\SO_50256566.cmd
#echo off
Set "Src=M:\Production Schedule.xlsm"
Set "Dst=M:\gsdBackups"
:: Get date and time in a user settinhs/locale independent format
For /f %%Y in ('
powershell -NoP -C "(get-date).AddDays(0).ToString('yyyyMMdd_HHmm')"
') Do Set _DT=%%Y
:: get source and use for variable modifiers to get name extension separated
For %%F in ("%Src%") Do echo copy "%%~F" "%Dst%\%%~nF_%_DT%%%~xF"
Sample output:
> SO_50256566.cmd
copy "M:\Production Schedule.xlsm" "M:\gsdBackups\Production Schedule_20180509_1743.xlsm"
If it looks OK to you remove the echo in front of copy.

Related

To grep contents from a CSV/Text File using Autohotkey(AHK) Script

Can anyone please help me in writing a script in AHK based on below requirement.
Requirement:
I have a CSV/TXT file in my windows environment which contains 20,000+ records in below format.
So, when I run the script it should prompt a InputBox to enter an instance name.
Example : If i enter Instance4 , it should display result in MsgBox as ServerName4
Sample Format:
ServerName1,ServerIP,Instance1,Type
ServerName2,ServerIP,Instance2,Type
ServerName3,ServerIP,Instance3,Type
ServerName4,ServerIP,Instance4,Type
ServerName5,ServerIP,Instance5,Type
.
.
.
Also as the CSV/TXT file contains large no of records , pls also consider the best way to avoid delay in fetching the results.
Please post your code, or at least show what you've already done.
You can use a Parsing Loop with CSV as the delimiter, and make a variable for each 'Instance' who's value is that of the current row's 'ServerName'.
The steps are to first FileRead the data from the file, then Loop, Parse like so:
Loop, Parse, data, CSV
{
; Parses row by row, then column by column in each row.
; A_LoopField // Current value
; A_Index // Current loop's index
; Write a script that makes a variable named with the current value of column 3, and give it the value of column 1
}
After that, you can make a Goto loop that spams InputBox and following a command that prints out the needed variable using the MsgBox command, like so:
MsgBox % %input%

redirect the sql query will add all the files in that directory [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 5 years ago.
I have shell script as below
query="SELECT * from [myDB.myTable]"
echo $(date +"%Y-%m-%d %H:%M:%S")'|'land'|'Failure'|||'twice'|'$query > test.log
When I run above in some directory in my Unix PC, the test.log shows the result as below
2017-12-13 06:54:03|land|Failure|||twice| SELECT temp1.txt test tmp.log tt.sh from [myDB.myTable]
Actually I wanted query to be redirected as it is in the log file, instead it printed all the file names of that directory.
How can I fix this issue?
The dreaded pathname expansion strikes again! You can attribute that list of filenames to having a * in an unquoted expression.
echo "$(date +'%Y-%m-%d %H:%M:%S')'|'land'|'Failure'|||'twice'|'$query" > "test.log"
# ^ ^
cat "test.log"
> 2017-12-13 06:54:03|land|Failure|||twice| SELECT * from [myDB.myTable]
Use double quotes for your complete echo, the single quotes can be deleted:
echo "$(date +"%Y-%m-%d %H:%M:%S")|land|Failure|||twice|${query}" > test.log
I also have put curly braves in ${query}, that is not a solution for your problem but a good habit for future scripts.

PostgreSQL - ERROR: missing data for column "XX" SQL state: 22P04

I am trying to import TXT file into the postgreSQL database table, but I am getting an error:
ERROR:
missing data for column "bts_name"
SQL state: 22P04
My code is:
COPY indicadores2g (
Daily,
BTS_NAME,
SITE_CODE
)
FROM 'C:\Users\Public\Documents\GEO_2G_CELL.txt'
WITH CSV HEADER DELIMITER ' ' NULL AS '' ;
I know that the problem is in the txt file. In the txt file the last two line are blank (example), and when I remove them, the SQL run without problem.enter image description here
My problem is I need to import every day. Is there any rule to put in my SQL code to run without problems?
Another way to run without problems is: Open TXT in excel and save as CSV. Can I do this automatically?
Create simple batch (for example inpfixer.bat):
#echo off
for /f "delims=" %%a in (%1) do (
echo %%a
)
Then
COPY indicadores2g (
Daily,
BTS_NAME,
SITE_CODE
)
FROM PROGRAM 'inpfixer.bat C:\Users\Public\Documents\GEO_2G_CELL.txt'
WITH CSV HEADER DELIMITER ' ' NULL AS '' ;
Surely, inpfixer.bat should be available by PATH.
Disclaimer: Tested on the Wine.

Creating a $variable from a specific part of a txt file

I'm trying to get PowerShell to use a specific section of a text file as a $variable to be used later in the script.
With Get-Content and index I can get to the point of having a whole line, but I just want one word to be the variable, not the whole thing.
The alphanumeric code will always be in the same location exactly
line 5 (counting the first one as 0 of course) and the position in would be between the characters 22 to 30 (or the last 8 characters of that line).
I would like that section of the document to be identified as $txtdoc, to be used later in:
$inputfield = $ie.Document.getElementByID('input5')
$inputfield.value = $txtdoc
The txt file contains the following
From: *************
Sent: *************
To: *******************
Subject: *************
On-Demand Tokencode: 79960739
Expires after use or 60 minutes
this maybe?
$variable = ( gc mytext.txt )[5].substring(21,8)

how to set the output of a command to a variable in batch file

im having difficulties trying to set the output of the hostname command as a variable. what I want to do is to have the text file that is outputted to have the name of the computer so computer1.txt, computer2.txt etc, but i want to do it without making a temp file for example
set HNAME =`hostname`
this is what i have currently but the script i am using is being run on several computers at the same and i believe that the temp file that i create is causing issues with the names of the .txt files.
hostname >> hostname.txt
set /p HNAME=<hostname.txt
pause
echo hello > %HNAME%.txt
pause
You have to use the for command, something like this:
for /f "usebackq" %i in ( `somecommand` ) do set envar=%i
It's very painful. for /? at the command line for more information.