Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"Not only is God dead, but just try to find a plumber on weekends." -- Woody Allen


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

<sic49t$1pok$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!aioe.org!LTi+xflewmQTbbz7H4hZOQ.user.46.165.242.75.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Copying files with generated filenames in a directory from inside
teh AWKprogram
Date: Tue, 21 Sep 2021 10:14:53 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sic49t$1pok$1@gioia.aioe.org>
References: <7faa87f4-881f-4b0b-8e79-d25a638bfe75n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="59156"; posting-host="LTi+xflewmQTbbz7H4hZOQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Janis Papanagnou - Tue, 21 Sep 2021 08:14 UTC

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]
> }
> }
> '
>

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