Windows batch script to move lines from txt file to new file - windows

I have a load of text files with some html code in them. EG:
Some random text....
..
...
....
<tag1>some more random text</tag1>
....
...
..
I need to run a script to go through each text file and move each line between the tags to a new text file in the same folder, and remove them from the original.
So the end result would be one file with no <tag1> and another file with only <tag1>.
I hope I made myself clear enough. Is this at all possible?

IF (and that is a big, bolded, italicized, capitalized IF) you can guarantee that each <tag1>...</tag> tag appears on a single line with no other content on that same line, and there are no tag attributes to complicate things, then the answer is easy.
Edit - fixed a number of bugs, it actually works now :-)
#echo off
for %%F in (*.txt) do (
echo processing %%F
findstr /rc:"^ *<tag1>.*</tag1> *$" "%%F" >"%%~nF.tag1%%~xF"
findstr /rvc:"^ *<tag1>.*</tag1> *$" "%%F" >"%%~nF.new%%~xF"
>nul move /y "%%~nF.new%%~xF" "%%F"
)
The solution could be extended to handle tag attributes fairly easily.
But I seriously doubt your problem is really that simple. Valid HTML content can have a tag spread accross many lines, and there can be many tags on one line.
Windows native batch is pretty lousy at text processing in general, and even worse for HTML or XML. I strongly recommend getting a third party tool like gnu sed for Windows that has robust text processing. Or better yet, get a tool that is specifically designed to process HTML.

Related

for loop command with multiple variables

I am very new to command line and script language, and not so familiar with terms. I will try my best to explain my issue.
I am trying to edit/compress my ebooks in bulk process, and here comes the Calibre.
Calibre is the ebook editing software to make some changes to my epub files.
It's quite feature rich and in most cases easy to use, but only and biggest downside is whenever I edit/covert/read books in the GUI interface I first have to add books in the library, which involves making copy of every file into calibre's predefined directory. and on editor's GUI I can only edit one file at a time.
I have thousands of ebooks to edit, it will take days to do so, it's also bad for my storage space. Big NO NO.
Thankfully, it is possible to compress images in mass on command line interface with the help of useful plugin to asssist with handling bulk files.
for /r "C:\Users\foldername" %v in (*.epub) do calibre-debug -r "Editor Chains" "Compress Images" "%v" "%v.epub"
It works just as expected. it scans all epub files in that folder and its subfolders, and call the plugin to do its job - compress images - and save the output files in the same folder, adding the ".epub" extension to the name of original file name to avoid duplicate.(e.g. this is a book.epub --> this is a book.epub.epub)
Only problem is I have yet to find a way to save the output files in a different folder, with same file name. maybe there's something I am missing or it's just not possible.
If anyone knows how, please let me know.
I've tried:
for /r "C:\Users\foldername" %v & "C:\Users\newfoldername" %x in (*.epub) do calibre-debug -r "Editor Chains" "Compress Images" "%v" "%x"
... and it obviously failed, haha.

Create multiple URL shortcuts from a text file using a batch script [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 28 days ago.
Improve this question
Summary: I have a set of reports I must to run, and would like to create links to each report online. The problem I'm having is how to make a batch file which can successfully loop through the file containing my URLs, and make an internet shortcut for each URL with a name based on data from URL itself.
Example URL exactly as shown, each URL is a line in a text file, (one report per day of the year):
<https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-01;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal>
<https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-02;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal>
<https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-03;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal>
Breaking the URL down to understand the static vs dynamic data and how to manipulate it.
https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date= (static data)
2023-01-01 (dynamic data - format YYYY-mm-DD)
;end_date= (static data)
2023-01-31 (dynamic data - format YYYY-mm-DD)
;vehicle_ids=1103007;report_id=25;report_type=normal (static data)
I'm trying to make the URL shortcut names based off the line itself for example, with the URL https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-01;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal, I'd like to create a shortcut in directory named: C:\Users\UserName\OneDrive\Desktop\Folder for the links\01-01-2023 mileage report.URL. The filename of 01-01-2023 being dynamic based on the date stated in the URL itself.
Also I found out the hard way, the other day, this computer "%UserProfile%\Desktop\Anywhere Else" is not a valid directory, not sure if this is a new Windows 11 trait, (moved from Win 7), or a OneDrive issue. Either way, any old code I had that used the %UserProfile% variable is needing re-done, or I need to make a new environment variable.
Here are my issue(s):
I have no experience using for looping command, let alone the restrictions of batch when using special characters. I thought I'd best ask before goofing something up terribly (special characters in the text file containing all the URLS).
The URL can vary on length ,so I'm not sure how to extract the month/day/year to use in the filename correctly.
Batch is the only language I have any experience with, and that is limited too. So whilst I'm sure there are much better options to do the task I'm wanting, I don't know them, to try them.
What I have done:
I used spreadsheet to break the URL down, modify the dates for the entire year, concatenate the data in a manner which made a proper working URL for each day of the year, added a column for the filename, and Echo #Echo Off> To the filename, Echo Start (the URL)>> To the filename, Echo Exit>> To the filename.
At which point, I was able to copy the crude columns of code from the spreadsheet, and paste them into the Command Prompt, which created a batch file for each URL, (file name was based on date) like so:
#Echo OFF
Start "" "https://app.gomotive.com/en-US/#/reports/ifta-distance/vehicle;start_date=2023-01-
01;end_date=2023-01-31;vehicle_ids=1103007;report_id=25;report_type=normal"
Exit
Making a working batch file for each link but surely not the right way to do it. (It's not the first time I've used a spreadsheet to break apart a directory or URL and re-constitute it for a batch file).
However I feel making actual internet shortcuts would be a much better way to do this across all forms of devices, (I think one of the people who may look at the reports from time to time use MacOS, and I'm assuming a batch file to "start" the URL won't work on their Mac, let alone not being the most friendly way of doing the job.
What my mind is telling me so far to do, (this is the batch file in my mind at the moment):
#Echo Off
CLS
REM Batch file create URLs based on text file
REM Filenames based on URL data from line in file
REM File containing URLs C:\Users\UserName\OneDrive\Desktop\Report Creation\FileNameOfURLS.txt
REM Save directory C:\Users\UserName\OneDrive\Desktop\Report Creation\All URLS Here
for /F "tokens=*" %%A in (C:\Users\UserName\OneDrive\Desktop\Report Creation\FileNameOfURLS.txt)
do (Echo [InternetShortcut] > "C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL"
echo URL=%%A >> "C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL"
echo IconFile=C:\Program Files\Google\Chrome\Application\chrome.exe >>
"C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL"
echo IconIndex=0 >> "C:\Users\UserName\OneDrive\Desktop\Report Creation\%Option1%.URL"
)
Exit
But I'm lost on how to extract the mm-DD-YYYY from the URL in the text file, and set it as the "option1", as I feel there is need for a second for command somewhere, to make this possible.
I have not ran my potential code, as shown above, through this system yet as I'm not sure how to make the filenames based on the data in the URL of the file filenames.txt, and I'm not sure this is even possible.
I did break the URLs down (365x) in spreadsheet, re-constitute them with proper dates, and make a column for filenames based on those dates, where I was able to further concatenate lines like Echo #Echo Off>FileName(s).bat, Echo Start "" "The URL with updated dates">>FileName(s).bat and Echo Exit>>FileName(s).bat, effectively creating a crude batch file containing the proper link, and having a correct name based on the date of the URL via the Command Prompt. However, making 365 batch files per year, and being locked to only Windows OS, not able to run those batch files on my phone either, makes them half as useful as if they were a .url file which any device can open.
Here is how I would think, based upon your provided text file content, and your unattempted code, it could be done:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "URLFile=C:\Users\UserName\OneDrive\Desktop\Report Creation\FileNameOfURLS.txt"
Set "SaveDir=C:\Users\UserName\OneDrive\Desktop\Report Creation"
For /F "UseBackQ Delims=<>" %%G In ("%URLFile%") Do (
For /F "Tokens=3 Delims=;=" %%H In ("%%~G") Do (
Echo [InternetShortcut]
Echo URL="%%~G"
Echo IconFile="C:\Program Files\Google\Chrome\Application\chrome.exe"
Echo IconIndex=0
) 1>"%SaveDir%\%%H.url"
)
As you had assumed, an additional for loop was needed.
Please be aware however, that it is not very likely that your phone, or a Mac will have a copy of chrome.exe in the location stipulated in the IconFile entry. So you may be better advised to miss out those two Icon* keys, or at least their values.

Windows cmd, file rename and rearange

I'm a biologist, and sometimes I have to work with quite a lot of files, which are arranged not in an optimal way by someone.
Can you give me a tip on the following task?
There are folders
D:\.......\folder1\subfolder(1....)\subsubfolder(1.....) \SOMEtext_exactword(1.....)
(the last one are files without extension)
Can I create in windows cmd something like
D:\.......\folderEXACTWORD1\(subfolder1_subsubfolder1_SOMEtext_exactword1,
subfolder2_subsubfolder2_SOMEtext_exactword1,
subfolder3_subsubfolder3_SOMEtext_exactword1)
(the last one are files without extension again)
Meaning to find all files, containing "string" in their name in few sub folders, copy them into the specific folder and rename them, so they contain their parent file location in their name now.
I guess there should be a way to do it in a linux, though I am not a linux user and not a good (not at all actually) coder myself. Though I can handle some work, so I would be happy if someone can give me a tip whats the easiest way to do what I want. Or what can I search - I tried to search, but maybe I am missing some right words to look for.
Your question is very unclear, but here is more or less something that does this, you have to still define a few more items to make sure your find the correct files in locations and move them accordingly.
#echo off
setlocal enabledelayedexpansion
set mydir="D:\somedir\somedir\someotherdir\
for %%a in (%mydir%) do set "name=%%~nxa"
echo %name%
for /r %%i in (*string*) do (set "file=%%i"
echo move "!file!" "C:\somedir\!name!")
)
)
endlocal
Well. Someone elsewhere has given me advice and it worked. In total commander, you can do it with multi-rename tool and flags [P] and [G] which will allow you to add parent and grandparent directory name.
Well. I guess there is nothing wrong with having this topic with answer from myself, but I will be ok if it is deleted :)
Though I will be still interested if it can be done from cmd, just out of curiosity.

Batch FOR Loop Inserting a special character

Im trying to run this Batch FOR Loop:
FOR %%f in (C:\folder\*.dwg) do start /wait c:\”program
files"\Autodesk\”AutoCAD 2014"\acad.exe "%%f" /b c:\Script\cgatt.scr
But for some reason when I run it instead of outputting c:"program files", it outputs a weird specialcharacter in place of the double quote:
Can anyone see what I'm doing wrong there?
The problem is with your first double quote in c:\”program
files"\Autodesk\”AutoCAD 2014"\acad.exe
It needs to be changed to a " like the other one is. The curved quotes are what is known as "smart quotes," which get added by some text editors automatically. Generally this can be avoided by coding batch scripts in text editors like Notepad or Notepad alternatives. There may also be an option in your text editor to turn smart quotes off. I highly recommend doing this, or else your scripts will continue to break.

Dos dir mask, want "*.xxx" and not "*.xxxzz"

In my directories, I have file names like *.xxx and also *.xxxzz
When I do dir /s/b "*.xxx" I get *.xxxzz files in my list. I do NOT get these results in a "Take Command" console, but I do in a cmd console.
How do I get cmd to give me only *.xxx files?
With the DIR command, when you specify a mask containing an extension of exactly three characters, you will get matches of files that contain extensions with three or more characters, so long as the first three characters match the extension you originally specified.
I have no idea why it works this way, but at least the behavior is consistent nearly everywhere in the Windows API where you can specify a file search pattern. I can only assume it has something to do with support for long file extensions (i.e., file names that don't comply with the old DOS 8.3 rule).
But, you can get around the behavior in two ways:
A mask that specifies a file extension with one, two, or more than three characters will return only files with extensions of exactly the specified length.
So, for example, dir /s/b "*.xx" will give you only files with the extension .xx, and dir /s/b "*.xxxzz" will give you only files with the extension .xxxzz.
You can use the question mark wildcard character, instead of the asterisk. Asterisks mean "replaced by zero or more characters", while question marks mean an exact substitution of the question mark with a single character.
I suspect you're running into a problem because of the way Windows (older versions, at least) generated a short 8.3 filename to improve compatibility with old programs. You can probably confirm this by doing dir /x *.xxx in the directory where your *.xxxzz files exist.
I'm not sure if there's a way around it from the limited Windows command line tools. There should probably have been a switch on the dir command to force consideration only of long filenames, but I don't see one.
You may be able to solve your problem by disabling short filenames on that volume, if you're sure you don't need them for any ancient software you're running.
I haven't tried that myself, so maybe the short names already generated will continue to exist after you follow those instructions. If so, you might be able to fix it by copying the tree you're working with to a new location.
The fact is that unless the system has been set up to not generate 8.3 names, every file or directory with a long filename will also have an 8.3 alias. At least one - with some of the warped constructs in use in later editions, there many be many aliases.
Academically, since it's a matter of opinion (and hence outside of SO's bailiwick) it could be argued that your alternative command processor is not producing the correct results since it apparently ignores the short filename. Which is "correct" is debatable - what suits in one application may not in another. The easiest and most logical way of course is to have an option - but the chances of the major player in the debate incorporating such a facility at this stage amount to well,Buckley's
Here's a routine that may suit. It's not bullet-proof as it will have problems with some "poison characters" (those with special meaning for the standard command-processor cmd.exe(A windows application that emulates and enhances many of the facilities available in DOS, and normally, though technically-incorrectly, called "DOS" by the politically-incorrect for the sake of brevity.))
#ECHO Off
SETLOCAL
SET "mask=%~1"
IF "%mask:~-4,1%"=="." ECHO(%mask:~-3%|FINDSTR /L "* ." >NUL&IF ERRORLEVEL 1 (
FOR /f %%a IN ('dir /s/b "%mask%"') DO IF /i "%%~xa"=="%%~sxa" ECHO(%%a
GOTO :EOF
)
dir /s/b "%mask%"
GOTO :EOF

Resources