String Manipulation in Batch file - string

I'm running a FOR loop to retrieve the (absolute) path name for ALL *.properties file in the root folder: "C:\ExecutionSDKTest_10.2.2\". Now, I'm trying to slice the path name to only result in the file name. e.g. if absolute path is "C\ExecutionSDKTest_10.2.2\Test-101" I only want the "Test-101" part (w/out the quotes of course) I have:
FOR %%G IN (C:\ExecutionSDKTest_10.2.2\*.properties) DO
(
REM Ignore "C:\ExecutionSDKTest_10.2.2\" ??
java -jar %1 %G:> Logs\%%G.log
)
So the absolute file path name is stored in G, but I'd only like the file name. How can I achieve this goal?

You can use
%%~nG
to get just the filename, or
%%~nxG
if you want the filename and extension.

Related

Import title of files containing string into a text document using batch

I am trying to create a batch script that imports the titles of text files in a folder that contain a specific string into a new test file.
This is what I have achieved so far. It imports every text file into the document and if it contains the string, it will put the contents of that file under the title. I just want the title of the file to be imported. Thank you
find "test" dir *.txt > output.txt
OUTPUT:
---------- ALSO_CONTAINS.TXT
test.
---------- CONTAINS.TXT
This is a test
---------- CONTAINS_ASWELL.TXT
This is also a test
---------- SHOULD_FAIL.TXT
Instead of using find, use findstr instead, which if you read the usage information from findstr /? at the Command Prompt, will show you a /M option, (which prints only the filename if it contains a match)
FindStr /IM "test" *.txt>output.txt
Just in case there's an issue with your %PATH% and/or %PATHEXT% environment variables, you could also use:
"%__APPDIR__%FindStr.exe" /IM "test" "*.txt">"output.txt"

Python3 replace part of file name of all files and directory name in a directory

I have 4 files (with different extensions) in a directory as shown below.
Test1234_`enter code here`Dir (Directory)
FileA_1234_test.dat
FileAAB_1234_test.log
FileABCD_1234_test.dbm
FileABCDE_1234_test.jdt
I want to replace part of directory and file names as shown below
Test6789_Dir (Directory)
FileA_6789_test.dat
FileAAB_6789_test.log
FileABCD_6789_test.dbm
FileABCDE_6789_test.jdt
How can we achieve this in Python3? I have no idea to do this.
I am not sure what you want to do.
maybe something like this
def replace_names(path, str_to_replace='1234', replacement='6789'):
import os
for filename in os.listdir(first_path):
if '_'+str_to_replace+'_' in filename:
new_filename = filename.replace(str_to_replace, replacement)
os.rename(filename, new_filename )
os.rename(path, .replace(str_to_replace, replacement))
#now, run it...
replace_names('Test1234_Dir')

BASH: How to copy the name of the file and insert it into the text using script?

So I need to create a lot of text files.
Each of the text file is named AAAAA.txt, BBBBB.txt, CCCCC.txt and etc etc.
Within each text file, all the content is as follows:
1.Copy "to-be-replaced".txt into the folder EXCLUSIVE.
2.Copy the gs file to replace the existing gs file.
3.The .projectdata should also be copied to the correct path.
So, I need to write a script, that copies the name of the file (AAAAA, BBBBB, and so on) and then place it in the "to-be-replaced" within its content.
How can I do that? need some idea please.
Thank you~~
MT32
Use a HERE document which expands variables if the delimiter isn't quoted:
#!/bin/bash
for char in {A..Z} ; do
filename=$char$char$char$char$char.txt
cat <<EOF > $filename
1.Copy $filename into the folder EXCLUSIVE.
2.Copy the gs file to replace the existing gs file.
3.The .projectdata should also be copied to the correct path.
EOF
done

String manipulation - getting file name from absolute file path

I have a variable containing a path to a file, that I obtain from the tk_getOpenFile function, the $file variable would be something like this:
/home/usr/Documents/Plugin-2-Linux.pdpk
I need some sort of split to get only the Plugin-2-Linux. Please note that the path may not be the same every time. So what I need is to get the string between the last / and the .pdpk and put it in another variable: $filename.
set filename [file rootname [file tail $file]]
file tail returns the part after the last / (not counting trailing /s), and file rootname the part before the last ..
man page for file

How to shorten the length of the path on the one folder in Matlab?

How to shorten the length of the path on the one folder in Matlab?
i.e. I want one directory up.
For example I have 'C:/mydir/folder1/folder2' I want 'C:/mydir/folder1'
If you have the folder path in a string, you can use the function fileparts:
currentFolder = pwd;
parentFolder = fileparts(currentFolder);
Note that this won't work if the folder path string ends in a file separator character (i.e. '/' or '\').
If you simply want to change to the parent directory of the current working directory, use cd:
cd ..
% or
cd('..')
We could also use Java builtin functions:
char(java.io.File(pwd).getParent())
also the Apache Commons IO library that ships with MATLAB:
char(org.apache.commons.io.FilenameUtils.getFullPath(pwd))

Resources