Jump to content

Wikipedia:Reference desk/Archives/Computing/2021 September 13

From Wikipedia, the free encyclopedia
Computing desk
< September 12 << Aug | September | Oct >> September 14 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


September 13

[edit]

Batch file to store versions of files

[edit]

In Windows, I want to have a batch file to store some files in their current form and not overwrite them when I run it again. That is, it keeps both sets of files with perhaps the time and date encoded in the file name.

I've thought of using a zip file, but I don't see any way to have it encode the date and time in the filename. Alternatively, copying the files to a folder would work, if the name of the folder had the date/time encoded or some unique identifying feature (like a sequence number).

Is there an easy way to do this? (Sometimes I need to go back to the way the files were a couple of changes ago.) Bubba73 You talkin' to me? 03:45, 13 September 2021 (UTC)[reply]

You could use a version control system. Some will operate on your local machine and store compressed versions of your files in a subdirectory, and almost all of them are fully operational from a batch file. I've used GNU Bazaar in the past, although it's no longer being developed.-gadfium 05:04, 13 September 2021 (UTC)[reply]
A batch file can easily copy the file with the date and time as part of the saved filename or it could create a directory named after the date and time and copy a file or set of files into it
You could also use a local copy of Git as a simple version control system. It can be asked to track a set of files and allow you to see the history of the changes. There are user-friendly programs that mean you do not need to use magical commands — GhostInTheMachine talk to me 13:13, 13 September 2021 (UTC)[reply]
Thanks, most of the version-control systems are overkill (this is just for me on one computer). I'm looking into Git. Bubba73 You talkin' to me? 04:46, 14 September 2021 (UTC)[reply]

Removing a query string with .htaccess

[edit]

Hi! I am trying to write a .htaccess rule that removes any query string.

In other words

 https://www.example.com/index.html?a=b&c=d&e=f

should redirect to

 https://www.example.com/index.html 

The following .htaccess almost works:

 RewriteEngine On
 RewriteBase /
 RewriteCond %{QUERY_STRING} ^(.+)$
 RewriteRule ^(.*)$ https://www.example.com/index.html? [R=302,QSD,L]

It removes any query strings I add, but what it doesn't do is remove a single "?"

 https://www.example.com/index.html?

does not redirect. The question mark does not go away.

I tried replacing ^(.+)$ with (.*) and with . (a single dot) but it acts the same.

Is there a way to get rid of that last pesky question mark?

14:04, 13 September 2021 (UTC)2600:1700:D0A0:21B0:E5A6:A801:85C4:AF4D (talk)

What about ^(.*)$? + means 1 or more, while * means 0 or more. — LauritzT (talk) 18:43, 13 September 2021 (UTC)[reply]
Aren't you redirecting everything to "/index.html?". Have you tried removing the question mark from the substitution? -- zzuuzz (talk) 20:03, 13 September 2021 (UTC)[reply]
Yes, I am redirecting everything that has a query string to "/index.html". In my experience first you need to get the RewriteCond working right (redirecting on the URLs you want it to, not triggering on the URLs you don't want it to) and then and only then compose a RewriteRule that defines where you want it to redirect to.
I have tried (on the RewriteCond):
  • ^(.*)$
  • ^(.+)$
  • ^.*$
  • ^.+$
  • ^.$
  • (.*)
  • (.+)
  • .*
  • .+
  • .
I have tried (on the RewriteRule):
  • With and without ?
  • With and without QSD
I also tried a RewriteRule that redirects to nonexistant file "example.txt" on a noexistant domain name. If RewriteCond triggers you get a 404 error and can see in the address bar that it was looking for example.txt. If RewriteCond doesn't trigger you get whatever page you typed in the URL for.
In all of the above experiments the rewrite engine appears to think that {QUERY_STRING} has zero bytes with and without the first ?, and that {QUERY_STRING} has one or more bytes with anything (including a second question mark) after the first ?. What I can't do is find a RewriteCond that treats "?" and "no ?" differently. I suspect that I need to test some other variable to "see" that lone question mark.
21:19, 13 September 2021 (UTC)2600:1700:D0A0:21B0:E5A6:A801:85C4:AF4D (talk)
You could try %{THE_REQUEST} although you'll need to strip the "GET" or whatever from the beginning of the string. A comment: using "^.*$" and other patterns which can match a zero-length string in RewriteCond seems odd, as I would think the same effect would be achieved by just omitting the RewriteCond line altogether. RewriteCond shouldn't be needed if your RewriteRule works for all possible URLs. CodeTalker (talk) 17:04, 14 September 2021 (UTC)[reply]
Actually, how about "RewriteCond %{THE_REQUEST} \?"? This just tests if there is a question mark in the request line. CodeTalker (talk) 17:13, 14 September 2021 (UTC)[reply]