Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

All great discoveries are made by mistake. -- Young


devel / comp.lang.awk / Re: Copying files with generated filenames in a directory from inside teh AWKprogram

Re: Copying files with generated filenames in a directory from inside teh AWKprogram

<60f87554-ede5-4c63-b57a-2b81e4b3a19dn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=908&group=comp.lang.awk#908

  copy link   Newsgroups: comp.lang.awk
X-Received: by 2002:ac8:86:: with SMTP id c6mr10068477qtg.78.1632222985149;
Tue, 21 Sep 2021 04:16:25 -0700 (PDT)
X-Received: by 2002:a25:3894:: with SMTP id f142mr40823612yba.464.1632222984919;
Tue, 21 Sep 2021 04:16:24 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.awk
Date: Tue, 21 Sep 2021 04:16:24 -0700 (PDT)
In-Reply-To: <sic49t$1pok$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=31.165.67.106; posting-account=BtTavwoAAACMi3AFrA3U-JBERbxGUGhD
NNTP-Posting-Host: 31.165.67.106
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com> <sic49t$1pok$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <60f87554-ede5-4c63-b57a-2b81e4b3a19dn@googlegroups.com>
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
From: mohsen.owzar@gmail.com (Mohsen Owzar)
Injection-Date: Tue, 21 Sep 2021 11:16:25 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 277
 by: Mohsen Owzar - Tue, 21 Sep 2021 11:16 UTC

Janis Papanagnou schrieb am Dienstag, 21. September 2021 um 10:14:55 UTC+2:
> A couple comments, pick your options...
>
> Generally it's simpler to do the file manipulation in shell.
> (If you provide a terse example and clear description the folks
> (here or better in comp.unix.shell) will help you.)
>
> If possible, get rid of punctuation characters and spaces.
> Specifically get rid of the quotes as part of the file names.
> Keep punctuation in date/time and gsub() all other punctuation
> to become spaces. Then gsub() all sequences of spaces to single
> underscore (for example).
> Since the source file name still has spaces you need to quote the
> whole file name that you provide as argument to mv/cp as in
> filename = "\"" filename "\""
> To do the renaming either construct the shell commands and print
> them for execution awk '{printf ... }' | sh or use system(...)
> to do it
> cmd = "mv -i \"" filename "\" " newname
> To prevent name clashes of the transformed names memorize them in
> an array and (for example) add numbering if necessary
> if (newname in names) names[newname]++
> newname = names[newname] newname
> For the shell 'mv' command use shell option '-i' to create an
> error instead of overwriting existing files (just in case that
> even the numbering creates an already existing file name).
>
> There's probably even more hints necessary. A concise post with
> a clear example and concrete questions will help you to get your
> answers.
>
> HTH.
>
> Janis
>
> PS: And try to keep your line lengths in your posts <78 characters;
> this is still Usenet (not a webforum), search for Usenet Netiquette.
> On 21.09.21 05:33, Mohsen Owzar wrote:
> > Hi Guys
> > I'm working with Cygwin under Windows 10 I have written a program in
> > AWK / Sh to copy or rename some selected
> files, filtered by a string given by the user, from 01 to the length of
> the selected files by the filter.
> > Before you say that this task can be done with another software than
> AWK, why I used this, OK, I know a bit only AWK and Bash scripting, and
> if you know another and more effective method than this one, do not
> hesitate and offer your suggestion.
> > But the thing is after so many hours struggling with this stuff, I
> want to know, how can I get these two variables (old and new filenames)
> out of AWK to copy or move
> > When I see a video tutorial, I take screenshots sporadically when
> > some
> actions are done.
> > The thing is that the screenshots have generic names derived from
> > the
> date and the name of the video and therefore can be very long, as you
> can see in the list below:
> > ----------------------------------------------------
> > 2021-09-08 10_10_42-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
> > 2021-09-08 10_10_55-PyQt5 - Apply your own Style using CSS - second method - updated - YouTube.png
> > 2021-09-08 10_13_37-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
> > 2021-09-08 10_13_46-Python PyQt5 -5- Make your first interaction with buttons using Signals Slots - .png
> > 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 2021-09-15 09_19_39-Window.Window.Window.Window.png
> > 2021-09-15 09_19_54-Window.png
> > 2021-09-15 09_20_06-Window.png
> > ----------------------------------------------------
> > Due to his issue, I wanted to give the user the possibility, to run
> > my
> script with an argument, which is a part of the screenshot name. My AWK
> program picks this string and takes files containing this string and
> change their names to names beginning with two digits for 01 to the
> length of the selected files and concatenated with the given string.
> > For example:
> > 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > with the given string as: " Button Flashing" is changed to
> > 01_Button Flashing.png
> > 02_Button Flashing.png
> > Now my problem:
> > I have managed to get the string from the user and sort the files in the directory by time and this string, and to get the old_file and new_file names.
> > I'm struggling since a couple of days to copy or move these files from old names to new names, vain and futile. It brings, all the time, that something is wrong with the destination and I have to check with "cp –help".
> > I think one problem is due to the fact that I'm using Linux commands (Cygwin) under Windows (CR/LF problem).
> > Another problem is that the filenames have spaces, and therefore I had to replace them forwards and backwards with some Characters. And the error messages after running the script has to do with this issue.
> > Because I found after a couple of hours, the given string had one character more than I need (I think CR/LF). Therefore, I had to get rid of the last character, as you can see in my code.
> > I have tried all combinations with system() command or print, no success. I hope someone can help me with this. I don't see the jungle because of all the trees.
> > Any help would be very appreciated.
> > Here is the output of the script:
> > &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
> > $> Rename_Screenshots_For_String.sh "Button Flashing"
> >
> > ================================================
> > Date and given arguments
> > ================================================
> > Generated on: 2021_09_21.05_05
> > TOP = /cygdrive/c/Users/m.owzar/Desktop/teesst
> > String = Button Flashing
> > DATUM = 2021_09_21.05_05
> > ================================================
> > *** 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube..png --> 01_Button_Flashing.png
> > *** 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube..png --> 02_Button_Flashing.png
> >
> > 2 Files were found!
> > --------------------------------------------
> > 71 : 2021-09-10 07_08_53-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 22 : 01_Button_Flashing.png
> > /bin/cp: zusätzlicher Operand 'Button'
> > „/bin/cp --help“ liefert weitere Informationen.
> > 71 : 2021-09-10 07_12_40-Make Button Flashing _ PyQt5 Tutorial - YouTube.png
> > 22 : 02_Button_Flashing.png
> > /bin/cp: zusätzlicher Operand 'Button'
> > „/bin/cp --help“ liefert weitere Informationen.
> > &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
> > And here is my code:
> > #! /usr/bin/bash
> > # Filename: Rename_Screenshots_For_String.sh
> > #########################################################
> > String=$1
> > DATUM=`date +"%Y_%m_%d.%H_%M"`
> > echo ""
> > echo "================================================"
> > echo "Date and given arguments"
> > echo "================================================"
> > echo "Generated on: " $DATUM
> > # =======================================================
> > ls -rt | grep -i "$1" | awk -v TOP="$(pwd)" -v String="$String" -v DATUM="$DATUM" '
> > ########################################
> > ## Begin Part
> > ########################################
> > BEGIN {
> > print "TOP = " TOP
> > print "String = " String
> > print "DATUM = " DATUM
> > print "================================================"
> > }
> >
> > ########################################
> > ## Main Part
> > ########################################
> > {
> > if ($0 ~ / /) {
> > A = gensub(/ /, "_XXX_", "g", $0)
> > Arr_A[++N] = A
> > }
> >
> > B = gensub(/_XXX_/, " ", "g", A)
> > Arr_B[++M] = B
> > }
> >
> > ########################################
> > ## End Part
> > ########################################
> >
> > END {
> > Str = gensub(/ /, "_", "g", String)
> >
> > for (i = 1; i <= M; i++) {
> > old_file[i] = Arr_B[i]
> >
> > K = split(old_file[i], ARRAY, ".")
> > Extension = ARRAY[K]
> >
> > Str_2 = substr(Str, 1, length(Str)-1)
> > new_file[i] = sprintf("%02d_%s.%s", i, Str_2, Extension)
> >
> > printf "*** %s --> %s\n", old_file[i], new_file[i]
> >
> > }
> > printf "\n%s Files were found!\n", M
> > print "--------------------------------------------"
> >
> > for (i = 1; i <= M; i++) {
> > print length(old_file[i]), ": ", old_file[i]
> > print length(new_file[i]), ": ", new_file[i]
> >
> > system("/bin/cp -T " old_file[i] " " new_file[i])
> > # print ""old_file[i]"" > new_file[i]
> > }
> > }
> > '
> >
Hi Janis,

After your suggestion I only needed two change in one part
of my code in "system() command".
No more changes were needed.
And I got the correct behavior as I wanted.

::::::::::::::::::::::::::::::::::::::::::
for (i = 1; i <= M; i++) {
print length(old_file[i]), ": ", old_file[i]
print length(new_file[i]), ": ", new_file[i]

cmd = "cp -i \"" old_file[i] "\" " new_file[i]
system(cmd)
::::::::::::::::::::::::::::::::::::::::::
Other question in this regard
If I want to ask user to copy, rename or abort (Cc, Rr or Nn)
before invoking the system() command
I used the following snippet code

::::::::::::::::::::::::::::::::::::::::::
for (i = 1; i <= M; i++) {
print length(old_file[i]), ": ", old_file[i]
print length(new_file[i]), ": ", new_file[i]

printf "Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:"
getline answer < "-"

answer = tolower(answer)

if (answer == "c")
cmd = "cp -i \"" old_file[i] "\" " new_file[i]
else if (answer == "r")
cmd = "mv -i \"" old_file[i] "\" " new_file[i]
else
print "You ommited Copying / Renaming files!"

system(cmd)
}
::::::::::::::::::::::::::::::::::::::::::
It doesn't stop at the line containing the "getline" command and waits for user answer.
It goes through all if, else if and else part and gives out
only the message from the else part, e.g.

Do you want to Copy [Cc] or Rename [Rr], otherwise [Nn]:You ommited Copying / Renaming files!

can you also help me out of this dilema?

Regards Mohsen

SubjectRepliesAuthor
o Copying files with generated filenames in a directory from inside teh AWKprogram

By: Mohsen Owzar on Tue, 21 Sep 2021

26Mohsen Owzar
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor