I am trying to export a txt file in ANSI using this command:
sqlcmd -S %1 -d %2 -U %3 -P %4 -h-1 -W -f 1252 -i .\filename.sql -o %5\filename.txt
But when I open Notepad+ the encoding is still UTF-8.
Any idea how to have the file exported in ANSI encoding?
Related
I have a set of md files, some of them are utf-8 encoded, and others are not (windows-1256 actually).
I want to convert only non-utf-8 files to utf-8.
The following script can partly do the job:
for file in *.md;
do
iconv -f windows-1256 -t utf-8 "$file" -o "${file%.md}.🆕.md";
done
I still need to exclude the original utf-8 files from this process, (maybe using file command?). Try the following command to understand what I mean:
file --mime-encoding *
Notice that although file command isn't smart enough to detect the right character set of non-utf-8 files, It's enough in this case that it can distinguish between utf-8 and non-utf-8 files.
Thanks in advance for help.
You can use for example an if statement:
if file --mime-encoding "$file" | grep -v -q utf-8 ; then
iconv -f windows-1256 -t utf-8 "$file" -o "${file%.md}.🆕.md";
fi
If grep doesn't find a match, it returns a status code indicating failure. The if statement tests the status code
I have a log file encoding with gbk, I have to read the data like this:
tail -n 2000 nohup.out | iconv -f gbk -t utf-8
but when I use tail -f it will print nothing:
tail -f nohup.out | iconv -f gbk -t utf-8
In a similar situation I use a script that reads each line and convert. In your case:
tail -f nohup.out | iconv.sh
#!/bin/bash
#iconv.sh
IFS=''
while read line
do
echo "$line" | iconv -f gbk -t utf-8
done < "${1:-/dev/stdin}"
I want to remove/trim the leading/left white space which are as a result of me hiding headers and footers from my PostgreSQL query using Windows batch. I am not sure whether these are white spaces or tabs.
My SQL query:
psql -d databasename -p portname -U username -t -f filename -o "C:\text.txt"
I am not aware of any other way to do this since my SQL is a multi line query and I am not sure if we can do this using -c.
Previous the result was something like this:
After removing the header:
So as you can see there is a white space here and I want to remove it.
Can someone please help me with this?
Have a look at the -t and -A psql parameters:
-t removes headers and footers from the results
-A switches off aligned mode (which is most likely where your whitespace is coming from - alignment into columns).
So the command should look something like the following:
psql -d databasename -p portname -U username -t -A -f filename -o "C:\text.txt"
So, basically, you shouldn't need to modify the resulting file - you can modify your psql command to get results in a format you want.
Here is an hybrid script (batch\vbscript) to trim a string left and right :
#echo off
Set "VAR= abc#abc.com "
echo My Variable before the Trim Function VAR="%VAR%"
Call :Trim "%VAR%"
echo(
echo My Variable after the Trim Function VAR="%VAR%"
pause>nul & exit
::*************************************************************************
:Trim <String>
(
echo Wscript.echo Trim("%~1"^)
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do (
set "VAR=%%a"
)
exit /b
::**************************************************************************
I am running a program on my machine that is triggered by running a .bat file. Right now, I am manually modifying the .bat to point to specific files/folders before running the script, which only takes a few minutes since I am running this on 3 to 4 files at a time. In the very near future, I am going to need to run this script on groups of files ranging from 200 to 500. Manually trying to edit the .bat file each time would be a nightmare.
The finished .bat would look like:
cd\[rootfolder]
mkdir Output
cd\folderpath\to\program
this.is.the.program.exe -i "[rootfolder]\[filename1].pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password
this.is.the.program.exe -i "[rootfolder]\[filename2].pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password
this.is.the.program.exe -i "[rootfolder]\[filename3].pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password
this.is.the.program.exe -i "[rootfolder]\[filename4].pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password
What I would like to do is create another script, .ps or .bat, that will take a list of filenames from a .txt file (dir /b output) and add the information from above in the correct place.
cd\[rootfolder]
mkdir Output
cd\folderpath\to\program
would only occur in the head of the script; This can be done in another way, but if it is included, that is fine...
this.is.the.program.exe -i "[rootfolder]\
would be added before each filename in the .txt file; I can manage this so far with a search/replace operation...
.pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password
would be added behind the filename. This is where I am having the issue!
The only similarity to the files will be the beginning of the filename; such as "Text_", which is why I can do a search/replace operation. The ending of the files are completely random and could be alpha, numeric or symbols, and could be of any character length.
I guess my question would be:
Is there a way to insert text into a .txt file by line position or something similar? Behind the last character on each line?
try this:
cd /d "rootfolder"
md output
for %%a in (*.pdf) do "folderpath\to\program\this.is.the.program.exe" -i "%%~a" -r "rootfolder" -o "rootfolder\Output" -u username -p password
or this:
cd /d "rootfolder"
md output
cd /d "folderpath\to\program"
for %%a in ("rootfolder\*.pdf") do "this.is.the.program.exe" -i "%%~a" -r "%%~dpa" -o "%%~dpaOutput" -u username -p password
In powershell you'd probably do something like this:
$rootfolder = 'C:\path\to\rootfolder'
$outputfolder = Join-Path $rootfolder 'Output'
$programfolder = 'C:\program\folder'
$filelist = 'C:\path\to\files.txt'
$user = 'username'
$pass = 'password'
if ( -not (Test-Path -LiteralPath $outputfolder) ) {
New-Item -ItemType directory -Path $outputfolder
}
Set-Location $programfolder
Get-Content $filelist | % {
.\this.is.the.program.exe -i (Join-Path $rootfolder $_) -r $rootfolder `
-o $outputfolder -u $user -p $pass
}
cd\[rootfolder]
mkdir Output
cd\folderpath\to\program
for /f "delims=" %%a in (
' the dir /b command that generates the .txt file '
) do (
this.is.the.program.exe -i "[rootfolder]\%%a.pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password
)
should execute this requirement.
I have a directory with about 50 wav files that I need to convert to caf, because AudioServicesCreateSystemSoundID() returns an error for some of them (but not all).
Here's an example of the command I've used successfully for a single file:
afconvert -f caff -d LEI16#44100 -c 1 whistle.wav whistle.caf
How do I do this quickly - not one-by-one for each file?
Similar approach for bash: for i in *.wav; do afconvert -f caff -d LEI16#44100 -c 1 $i ${i%.wav}.caf; done
On Windows, use the %~ni syntax.
for %i in (*.wav) do afconvert -f caff -d LEI16#44100 -c 1 %i %~ni.caf
for file in *.wav; do afconvert -f caff -d LEI16#44100 -c 1 "$file"; done
Hit the Return key directly after done.
Simple :)
For the people who are using OSX and are a bit afraid of Terminal scripts I created a little application with Automator, this application converts the files you select.
Download here
found this:
##
## Shell script to batch convert all files in a directory to caf sound format for iPhone
## Place this shell script a directory with sound files and run it: 'sh converttocaf.sh'
## Any comments to 'support#ezone.com'
##
for f in *; do
if [ "$f" != "converttocaf.sh" ]
then
/usr/bin/afconvert -f caff -d LEI16 "$f"
echo "$f converted"
fi
done
Customize the afconvert options then save it as a text file called 'converttocaf.sh', place it in the directory of files you want to convert and run it from Terminal.
It works with files with spaces in their names.
If you are dealing with filenames that contain spaces on Linux, try the following code:
SAVEIFS=$IFS
IFS=$(echo -en "\n\b"); for i in *.wav; do afconvert -f caff -d LEI16#44100 -c 1 $i ${i%.wav}.caf; done
Python script on OSX. Default data format of the .caf is ima4. Default directory is '.'
Make a file called wav2caf.py, put it in your path, make it executable, fill it with:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import argparse
import glob
def main():
# handle command line args
parser = argparse.ArgumentParser(description='A program that converts .wav to .caf files.', formatter_class=argparse.RawTextHelpFormatter)
parser.add_help = True
parser.add_argument('-f', '--folder', dest='folder', type=str, default='.', help='folder of files to convert')
parser.add_argument('-d', '--data', dest='data', type=str, default='ima4', help='data format of .caf')
args = parser.parse_args()
# process files in folder
os.chdir(args.folder)
for filename in glob.glob("*.wav"):
name, ext = os.path.splitext(filename)
command = 'afconvert -f caff -d ' + args.data + ' ' + filename + ' ' + name + '.caf'
os.system(command)
if __name__ == "__main__":
main()
Converts all .wav to .caf in current directory:
wav2caf.py
Converts all .wav to .caf in specified directory:
wav2caf.py -f Desktop/wavs/
Converts all .wav to .caf with data type 'aac ':
wav2caf.py -d 'aac '