Windows Batch File - Using Append with File Name that has spaces - scripting

I am creating a batch file to consolidate some hard coded text with a few of other existing text files.
for this I am using the below.
set "txtFile=.\text.txt"
call:Append "C:\test 123\test.txt" %textFile%
over here, when I execute it, it thros an error as it is not able to proceed with the path as it has spaces.
how should this be addressed.

I have no idea what your append batch file is doing, but you can simply use copy to concatenate two files.
It's not clear to me what the needs to be appended to what, but the following will append the contents of text.txt to C:\test 123\test.txt by writing everything to C:\test 123\test.txt.
set txtFile=.\text.txt
copy "C:\test 123\test.txt" /a + %txtFile% /a "C:\test 123\test.txt"
If you want a different output file, just change the last parameter.
Btw: it's better to not rely on a specific working directory
The following:
set txtFile=%~dp0text.txt
makes sure that the text.txt is used that is in the same directory as your batch file.

Related

Back up script issues

Not entirely sure why our script isnt backing up, i suspect that it may be something with the Robocopy. Ik robo copy is a sys32 file but looking at the script its shown to be apart of another path then followed by it is the correct path c:\windows\sys32.
So im looking at the two "copy "lines. lines 2&3. Is that normal formatting, because the robocopy is not in that path.
if exist "C:\windows\system32\robocopy.exe" Goto SkipPrep
copy"%logonserver%\netlogon\tools\robocopy.exe""C:\Windows\system32"
copy"%logonserver%\netlogon\tools\sleep.exe" "C:\Windows\system32\
cls
:SkipPrep
This a bat file

IntelliJ - File watchers: wrong output of macros

I am trying to setup a file watcher for scss files which is working on files with a filename not starting with _.
But if I have a file named _file_name.scss the output of any macros that include the filename will be file.name.scss.
The first _ is removed and following ones are replaced by ..
Even though in the insert macros selection tool I can see that the output when you select a macro is correct.
Like $FilePathRelativeToProjectRoot$ will display mypath/_file_name.scss in the selection tool but then my command from this file watcher will output mypath/file.name.scss.
Am I missing a parameter here ?
Full configuration:
For me, existing file names are not changed when using similar file watcher. But files with names starting the _ are not prettified, the main .scss that includes them is processed instead.
To avoid this, try adding COMPILE_PARTIAL=true variable to your file watcher settings:
Also, make sure that Track only root files is off.
See the comments in https://youtrack.jetbrains.com/issue/WEB-13459

Check if Windows batch variable starts with a specific string

How can I find out (with Windows a batch command), if, for example, a variable starts with ABC?
I know that I can search for variables if I know the whole content (if "%variable%"=="abc"), but I want that it only looks after the beginning.
I also need it to find out where the batch file is located, so if there is a other command that reveals the file's location, please let me know.
Use the variable substring syntax:
IF "%variable:~0,3%"=="ABC" [...]
If you need the path to the batch file without the batch file name, you can use the variable:
%~dp0
Syntax for this is explained in the help for the for command, although this variable syntax extends beyond just the for command syntax.
to find batch file location use %0 (gives full patch to current batch file) or %CD% variable which gives local directory

batch scripting: how to get parent dir name without full path?

I'm working on a script that processes a folder and there is always one file in it I need to rename. The new name should be the parent directory name. How do I get this in a batch file? The full path to the dir is known.
It is not very clear how the script is supposed to become acquainted with the path in question, but the following example should at least give you an idea of how to proceed:
FOR %%D IN ("%CD%") DO SET "DirName=%%~nxD"
ECHO %DirName%
This script gets the path from the CD variable and extracts the name only from it to DirName.
You can use basename command:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=$(basename "$FULLPATH")
You can use built-in bash tricks:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=${FULLPATH##*/}
Explanations:
first # means 'remove the pattern from the begining'
second # means 'remove the longer possible pattern'
*/ is the pattern
Using built-in bash avoid to call an external command (i.e. basename) therefore this optimises you script. However the script is less portable.

Is it possible to direct your output file having the same name as input file, that is to overwrite?

I would like to overwrite the name of input file with the same name of output file owing to limited disk space that I have in my system. Is it possible? I know this is not recommended, but I have the input files already backup. I will have a loop in a shell to do the cut command.
#!/bin/bash
for i in {1..1000}
do
cut --delimiter=' ' --fields=1,3-7 input$i.txt > input$i.txt
done
You could always use a temporary file to which you redirect, and then when you're sure everything went fine, you rename it to the original file.
some gnu utils commands have a -i option (such as sed) that allow you to change a file in place .....most of file filtering and editing (like cut) can be done using sed.
The shell will parse the command and handle the redirections first. When it sees "> afile" it will truncate "afile" and open it for writing. Your data is now destroyed. Then the shell hands the filename to cut which now has nothing to read.
This is how I learned:
some | pipeline < my_file > my_file.tmp
ln my_file my_file.bak # this is a hard link
mv my_file.tmp my_file
That keeps the original data in place for as long as possible.
If you're having disk space issues, you will have to read the input file into memory entirely.
In case of very limited disk space (disk quota) you could try to place a compressed source file in ram (/dev/shm) and use that as the source (uncompressing it to stdout and piping that to your script).