Batch remove parentheses and square brackets from file name [closed] - windows

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I would like to know how to remove parentheses and square brackets and what's inside of them in a list of files' names, using a .bat file
For example:
[Tag]File (1).mkv
to
File.mkv

The solution to your issue can be found in syntax-replacement. The first thing you want to do is have the batch file turn your file name into a variable. From there, you can use syntax-replacement with the set= to replace/remove parts of the variable.
The script bellow will remove [square brackets] and the contents inside them, and (parentheses) and the contents inside them.
Please keep in mind that this script bellow in an example to demonstrate syntax-replacement. It will not check/test (if) somethings in-front or behind. It expects you to have it formatted as [Tag]Name(#).ex - Change it to your needs.
#echo off
#setlocal
::Input your file location bellow.
call :edit "C:\Users\%username%\Desktop\[TAG]Hello(1).jpg"
goto :EOF
:edit
::Define file as variable
set file=%~1
set start=%~1
::Define file extension as variable
set ext=%file:*.=%
::Remove right square bracket and everything before it
set square=%file:*]=%
::Remove everything after left parentheses
set efile=%square%
set endbit=%efile:*(=%
call set result=%%efile:%endbit%=%%
::Remove left parentheses
set final=%result:(=%
::Add file extension and rename
ren %start% "%final%.%ext%"
exit

Related

How to remove double quotes without escape (^) in cmd/ batchfile? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
Note: as the OP is a new user, I'm editing the question with reproducible details so that it doesn't get closed. It's a good question, but was originally confusing.
The Windows username is: jhon^smith
ECHO %username%
output : jhonsmith
My expectation:
jhon^smith
If I use:
echo "%username%"
then the output is:
"jhon^smith"
How can I remove the double quotes but keep the caret, which is actually part of the username?
This should work:
for /f %a in ("%username%") do echo %a
output:
jhon^smith
(btw: It should be spelt john^smith but never mind.)
Explanation:
In the command echo %username%, cmd.exe replaces %username% with its actual content, so the command becomes echo jhon^smith. This is then parsed, the the ^ is processed as the escape character, so it replaces ^s with s.
In the command for /f %a in ("%username%") do echo %a, the quoted string is treated as a string without the quotes, and no parsing of ^ is performed. Type for /? for more details.

How to separate multiple parts of a line using ';' using shell script? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a '.GS3' file with multiple lines like this one:
0123456789 aaa.aaa3456
I want to separate it like this:
01234;56789 ;aaa.aaa;3456
I know the start and end of each part of the line. Is it possible to do this considering multiple lines? For example:
0123456789 aaa.aaa3456
aaaaabbbbbbbxxxxxxxwwww
Into
01234;56789 ;aaa.aaa;3456
aaaaa;bbbbbbb;xxxxxxx;wwww
.. but if it does have fixed lengths you can do it this way:
$ sed -r 's/^(.{5})(.{7})(.{7})(.{4})$/\1;\2;\3;\4/' test.txt
01234;56789 ;aaa.aaa;3456
aaaaa;bbbbbbb;xxxxxxx;wwww
I think .{5} is self explanatory. Due to the -r option the first group (.{5}) can be referenced by \1. It's a group due to ( and ).
The characters ^ and $ represent the beginning and ending of every line in the file test.txt.

Howto rename bunch of filenames which are calculated [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want rename bunch of filenames. The rename is based on the calculation of the filename. That means the actual filename + 3600 = new filename.
Important is that the underscore in the pid files have to stay.
Thanks in advance!
My system is Debian Stretch.
Actual Filename:
134235.error
134235_.pid
134235.tiff
13893.error
13893_.pid
13893.tiff
1.error
1_.pid
1.tiff
Rename to:
137835.error
137835_.pid
137835.tiff
17493.error
17493_.pid
17493.tiff
3601.error
3601_.pid
3601.tiff
for fname in *; do
echo mv -- "$fname" "${fname/*[[:digit:]]/$((${fname%%[^[:digit:]]*}+3600))}"
done
If everything looks ok, remove echo.
With Perl's standalone rename command. Some distributions call it prename.
rename -n 's/(\d+)(.+)/${\($1+3600)}$2/' *
If everything looks fine, remove -n.

Using a .bat file, I need to extract information after ")" punctuation from a variable and store it in another variable?

I have a variable from a text file.
set /p namz=<"playersname.txt"
Right now if I echoed this, it would echo the entire contents of the file.
The Contents of playersname.txt include the following,
theplayernameis:Player One
I need a code that I can use in .bat to help me to extract everything after the ":" from the .txt file while ignoring everything else before it, "Player One" then it needs to store it in another variable which I can call upon anytime I want it.
I've been racking my brain on this, trying to come up with a solution, been forum hopping for a long time, haven't quite found what I needed yet. Please help..
You have a single-line textfile, you need the second token after a delimiter that might be : or /:
for /f "tokens=2 delims=:/" %%a in (playersname.txt) do set namz=%%a
add every possible delimter to delims=

My perl script doesn't see the txt file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
My Perl program creates the file
10001.ICNTL.20160602.20160603.OPR.GAAP.PROD.PFI.PRE.txt
Then in method I have a code:
if ( -e $report ) {
# we parse the filet here is some code, at the end
{
else
}
print "*** Skipping \\NYNAS\NYNDS\VOL\DATA\INVACCT\FUND_RECS_PFI\10001.ICNTL.20160603.PROD.GAAP.PFI\10001.ICNTL.20160602.20160603.OPR.GAAP.PROD.PFI.PRE.TXT
}
I cannot understand why the script doesn't see the file. I've checked it several times letter by letter. Can it be because of the Upper case TXT, but in reality it is lower case?
Is your file 10001.ICNTL.20160602.20160603.OPR.GAAP.PROD.PFI.PRE.txt in directory \\NYNAS\NYNDS\VOL\DATA\INVACCT\FUND_RECS_PFI?
At a guess you're not escaping the file path correctly. Even if you use single-quotes, there is no way of representing the two leading backslashes in Uniform Naming Convention (UNC) paths without escaping at least one of them
Check the output of print $report, "\n" to see what you've really written
My preference is to use four backslashes at the start of the path string, like this
my $report = '\\\\NYNAS\NYNDS\VOL\DATA\INVACCT\FUND_RECS_PFI\10001.ICNTL.20160603.PROD.GAAP.PFI\‌​10001.ICNTL.20160602.20160603.OPR.GAAP.PROD.PFI.PRE.TXT';
print -e $report ? "Found\n" : "Not found\n";
And Perl allows you to use forward slashes in place of backslashes in a Windows path, so you could write this instead if you prefer, but paths like this aren't valid in other Windows software
my $report = '//NYNAS/NYNDS/VOL/DATA/INVACCT/FUND_RECS_PFI/10001.ICNTL.20160603.PROD.GAAP.PFI/‌​10001.ICNTL.20160602.20160603.OPR.GAAP.PROD.PFI.PRE.TXT';
Or another alternative is to relocate your current working directory. You cannot cd to a UNC path on the Windows command line, but Perl allows you to chdir successfully
chdir '//NYNAS/NYNDS/VOL/DATA/INVACCT/FUND_RECS_PFI' or die $!;
Thereafter all relative file paths will be relative to this new working directory on your networked system

Resources